0

I am trying to get some data via ajax but for some reason it gives errors in the console in Chrome and IE only. It works fine in Firefox.

Here is my code:

PHP

function get_data() {
     $data = array( 'value' => '50', 'type' => 'box' );
     echo json_encode($data);
     exit;
}

JS

$(".click").click(function() {
     var data = {
      action: "get_data"
     };

     $.post( "http://domain.com/", data, function(response) {
          var newResponse = $.parseJSON(response);
          console.log(newResponse);
     }); 
});

So this code works fine in Firefox but for some reason it doesn't work in Chrome and IE...In Chrome, it doesn't even get to the console.log function before it errors out.

Thanks for looking.

12
  • Can you post what your AJAX response contains? Perhaps there's something invalid being output by your PHP that is causing issues with the other browsers but FF is more forgiving of. Commented Oct 1, 2012 at 23:22
  • Set a breakpoint in Chrome in the first line of the callback function in $.post and you will see what's wrong. Commented Oct 1, 2012 at 23:23
  • If you comment out the two lines in your $.post callback, does the error go away? I'm wondering if it's coming from the $.parseJSON. Commented Oct 1, 2012 at 23:24
  • The errant answer was deleted, but I just wanted to point out that Javascript does not require semicolons as line terminators. They are optional. Commented Oct 1, 2012 at 23:24
  • The response is correct {"value":"50","type":"box"} in firefox...and yes the error goes away if I comment out the parseJSON line... Commented Oct 1, 2012 at 23:26

3 Answers 3

1

I guess it should work without .parseJSON. Just like this, at least it works for me:

$.ajax({
    url: "url...",
    type: "post",
    data: data,
    success: function (data) {
        if (data.Result) {
            data.value1;
            data.value2;
        } else {
            // do something else
        }
    }
});

data is just my type like { value1:'123', value2: '345', Result: true}

Sign up to request clarification or add additional context in comments.

Comments

0

Which version of jQuery are you using? Older versions are less forgiving about whitespace in JSON files that are returned

1 Comment

I believe the version is 1.7.2...can you please elaborate more on the white space? How is white space introduced if its created in PHP via array?
0

Ok after hours and hours of trying to figure out what is wrong, I finally figured out that there was an issue with a redirect of some sort on the ajax url. Not sure how that redirect got there but once I removed it, everything started to work. And by the way, it was not effecting Firefox is only because I was logged in to the site. I guess the redirect had some kind of check if a person is logged in, don't redirect the ajax url.

But in any case, I just like to share that in case someone else has the same issue as me where the code is correct and still not working.

And I like to just mention a big thanks to Felix for going out of his way to try and help me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.