AJAX in plugin wp_send_json() sending html

I wrote a plugin that calls a PHP function from JQuery and the php sends a json response back to the Jquery using wp_send_json(). The functions are all called successfully, but the json request sends a lot of html to the jquery function every time. How do I make the json sent by the php function only a specific message?

JQUERY:

jQuery( document ).ready( function() {

        jQuery( 'body' ).on( 'click', '.wpm_mail_link', function( e ) {

            var varData = 'name:[email protected]';      

      jQuery.ajax({
        type: "POST",
        action: "wp_ajax_send_email",
        url: my_ajax_obj.ajax_url,
        data: varData,
        success: function(data) {
          console.log('Ajax request successful');
          console.log(data.message);
        }
      });



        });

});

PHP:

    public function send_email() {
        $response = array(
        'message' = 'Sent',
        'ID'      = 1,
    );

    wp_send_json( $response );
    }

Topic json ajax plugins Wordpress

Category Web


Fixed it! I was getting a false positive with the ajax request. I thought it was going through but returning 'undefined' but I was actually using the wrong url.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.