简体   繁体   English

语法错误:JSON.parse:意外字符

[英]SyntaxError: JSON.parse: unexpected character

I have a JSON parse error that I can't debug.我有一个无法调试的 JSON 解析错误。 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:和 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 "在 Firefox 中,错误是“SyntaxError:JSON.parse:意外字符”,在 Chrome 中是“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.如果服务器正在发送有效的 JSON,则您不需要调用$.parseJSON ,因为 jQuery 会在检索响应时自动解析它。 I don't know the exact criteria, but if you set the Content-type: application/json header it definitely will.我不知道确切的标准,但是如果您设置Content-type: application/json标头,它肯定会。

This error can be caused by using single quotation marks ( ' ) instead of double ( " ) for strings.此错误可以通过使用单引号(引起' )而不是双( " )的字符串。

The JSON spec requires double quotation marks for strings. JSON 规范要求对字符串使用双引号。

See also:也可以看看:

https://stackoverflow.com/a/14355724/1461850 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, dataType: 'text', // 期望从 PHP 脚本返回什么,如果有的话 cache: false, contentType: false, processData: false,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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