0

I need some help here. I'm sending simple request to server, and the return I expected is JSON as data type. But when I checked in development tools console log I get "parsererror SyntaxError {}" and "parsererror".

How can I make this right? Below is the code.

JQuery

$(':submit').live('click', function() {

    $.ajax({

            type : 'post',        
            url: 'testJSON.php',
            beforeSend:console.log('sending...'),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function(data){

                console.log(data.status);
                // do magic
                },

            error: function(XMLHttpRequest, textStatus, errorThrown) {

                console.log(textStatus, errorThrown);

                },

            complete: function(XMLHttpRequest, status) {

                console.log(status);

                }

        });

    return false;
    });

and this is the testJSON.php

<?php

$data = array(

    "status" => 1,
    "firstname" => "foo",
    "lastname" => "bar",

);

header('Content-type: application/json; charset=utf-8" ');
echo json_encode($data);


exit();

?>

FYI I use the latest version of WAMP. Any help very much appreciated.

7
  • Please note that you have invalid JSON and you've posted everything except the JSON ;-) Commented Apr 29, 2013 at 11:03
  • @ÁlvaroG.Vicario I'm not sure what you mean by invalid JSON... if you mean why I sending request to server without JSON, simply because the data that needed to run the php script stored in $_SESSION[] Commented Apr 29, 2013 at 14:28
  • You said that you were getting parsererror when parsing JSON... Sorry if I misunderstood. Commented Apr 29, 2013 at 15:05
  • It's true. I try to preview the testJSON.php, then Copy text from the browser and paste it inside online JSON validator (jsonlint.com) and I get a Valid JSON... still don't know why the JS can't read it as JSON. Commented Apr 29, 2013 at 15:31
  • Do you save testJSON.php as UTF-8 with BOM or without BOM? Commented Apr 29, 2013 at 15:32

3 Answers 3

1

Set the header of the content to type of json... Here is an example of setting header type.

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

In jQuery 1.4 onwards the JSON data is parsed in a strict manner.

Any malformed JSON is rejected and a parse error is thrown.

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

1 Comment

I'm using jQuery 1.8.3. I try this solution but the code still not working. I run the preview of the php script the data successfully converted into JSON type, I don't understand why AJAX can't receive it, any idea?
0

remove " from here

header('Content-type: application/json; charset=utf-8" ');

should be

header('Content-type: application/json; charset=utf-8');

3 Comments

By setting dataType: 'json' the data is already (attemptedly) parsed
@GeenHenk Yeah I noticed that, thanks for correcting the mistake...:)
@GeenHenk I already removed it, but still get parsererror SyntaxError{} in my log ...
0

I tried your code and I got an error at the beforeSend parameter. The statements shall be encapsulated in a function:

...    
beforeSend: function() {console.log('sending...')},
...

After that everything worked. Maybe it also depends on the jQuery version. Which one did you use? I tried with versions 1.8.1 and upwards

1 Comment

I'm using V 1.8.3. and the code work for me.. the reason iI use this parameter so that I know the AJAX works for the first place (and by the way the code is ... beforeSend:console.log('sending...')... without function(){})

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.