SyntaxError:JSON.parse:意外字符 [英] SyntaxError: JSON.parse: unexpected character
问题描述
我有一个无法调试的JSON解析错误。代码如下:
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);
});
});
和JSON:
{
"status": "success",
"message": "A temporary password has been emailed to you."
}
在Firefox中,错误是SyntaxError:JSON.parse:unexpected character,在Chrome中它是Uncaught SyntaxError:Unexpected token C
In Firefox the error is "SyntaxError: JSON.parse: unexpected character", in Chrome it's "Uncaught SyntaxError: Unexpected token C "
有任何想法吗?
* 编辑:这在我的localhost设置上工作正常。 *
推荐答案
您无需致电 $。parseJSON
如果服务器发送有效的JSON,jQuery将在检索响应时自动解析它。我不知道确切的标准,但如果您设置 Content-type:application / json
标题,肯定会。
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.
这篇关于SyntaxError:JSON.parse:意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!