0

I'm developing a small plugin for a client and my ajax calls returns the whole page as json response. I cannot find the bug.

In the plugin main.php:

function __construct(){
  //..
  $this->PluginSetup();
}

function PluginSetup(){
  //..
  add_action( 'wp_ajax_the_sag_form', array( $this , 'the_sag_form' ) );
}

function the_sag_form(){
  $response = "hello";
  wp_send_json($response);
}

In my main.js:

function init_inpage_functions(){
    var PageData = new FormData();
    PageData.append('action', 'the_sag_form');
    $.ajax({
        method: 'post',
        url: ajaxurl,
        dataType: 'json',
        data: PageData,
        processData: false,
        contentType: false,
        beforeSend:function(data){

        },
        success:function(data) {
            console.log(data);
        },
        error: function(data){

        }
    });

}

What's wrong? In the response I just received the whole page. Thanks.

  • Returns the whole of what page? $reponse "hello"; will throw a parse error, as that's invalid syntax. – Milo Oct 15 '18 at 18:19
  • @Milo ok sure, that was a typo. Returns the html of the current option page. Any ideas? – huraji Oct 15 '18 at 19:30
  • 1
    Are you sure ajaxurl is correct? If you manually visit the url via your browsers address bar, do you see the correct response? – Milo Oct 15 '18 at 20:59
  • Yes the url is correct. Going crazy. I'm investigating on possible variable conflicts. – huraji Oct 16 '18 at 8:04
  • @Milo there was a conflict with another ajax function. It was checking on wp_doing_ajax() and responding to all ajax requests. Thanks for your support. – huraji Oct 16 '18 at 8:15
-1

you are return simple a string in response variable that is not legitimate on the next line you are returning wp_send_json expect an array but you pass a variable.

  • I don't think you got the point and what you say is incorrect. I fixed anyways as you can read in the previous comments. – huraji Oct 16 '18 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.