4

I have a JSON parse error that I can't debug. Code below:

$(document).on('submit', '#confirmreset', function(event) {
    event.preventDefault();

    var action_url = $(this).attr("action");
    alert_box_register("Resetting password...");
    console.log(action_url);
    var postData = $(this).serializeArray();
    console.log(postData);

    $.post(action_url, postData, function(data) {
        console.log(data);
        var obj = $.parseJSON(data);

        alert_box_register(obj.message);
    });
});

And the JSON:

{
    "status": "success",
    "message": "A temporary password has been emailed to you."
}

In Firefox the error is "SyntaxError: JSON.parse: unexpected character", in Chrome it's "Uncaught SyntaxError: Unexpected token C "

Any ideas?

*EDIT: This works fine on my localhost setup. *

10
  • 1
    That is valid JSON as presented, so I suspect that the real JSON data contains a character that is not making it across the translation. Commented Nov 6, 2013 at 22:32
  • How is the json being generated? Commented Nov 6, 2013 at 22:34
  • 1
    Where are you calling JSON.parse? Commented Nov 6, 2013 at 22:34
  • it happens within $.parseJSON(data), but i'm sure you knew that. Commented Nov 6, 2013 at 22:37
  • Don't parse the json inside the callback. Instead, tell jquery that it is json. $.post(action_url, postData, function(data){...}, "json") If your success stops working, that means your server isn't returning valid json. Commented Nov 6, 2013 at 22:38

3 Answers 3

18

You don't need to call $.parseJSON if the server is sending valid JSON as jQuery will parse it automatically when it retrieves the response. I don't know the exact criteria, but if you set the Content-type: application/json header it definitely will.

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

9 Comments

Thanks, so would you propose just 'var obj = $(data);'
No, just var obj = data, or even function (obj) { -- basically data is ready for use immediately
This creates Object {status: "error", message: Array[1]} (index):241 Uncaught SyntaxError: Unexpected token o
@alias51 that means your server isn't returning valid json.
Thanks. Was looking for answer, and this proved to be my problem as well. Trying to parse already parsed data. Since it is pre-paresed when usning json as type or getJSON as method.
|
0

This error can be caused by using single quotation marks (') instead of double (") for strings.

The JSON spec requires double quotation marks for strings.

See also:

https://stackoverflow.com/a/14355724/1461850

Comments

0
  1. Only put these=>

    dataType: 'text', // what to expect back from the PHP script, if anything cache: false, contentType: false, processData: false,

Comments

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.