3

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. *

alias51
  • 8,178
  • 22
  • 94
  • 166

3 Answers3

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.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

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

Lee
  • 29,398
  • 28
  • 117
  • 170
0
  1. Only put these=>

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