简体   繁体   中英

SyntaxError: JSON.parse: unexpected character

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

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.

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

  1. Only put these=>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM