AJAX in plugin wp_send_json() sending html

by brothman   Last Updated April 14, 2019 22:08 PM

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:foobar&email=bar@baz.foo';      

      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 );
    }
Tags : plugins ajax json


Answers 1


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.

brothman
brothman
April 14, 2019 21:38 PM

Related Questions


Wordpress - Call Ajax retrieve value with Json

Updated August 01, 2019 16:08 PM

Ajax store response json in variables

Updated September 23, 2017 14:08 PM