Javascript 需要帮助解决一个奇怪的json解析问题吗
我在解析从ajax调用(使用jQuery1.4.4)返回的格式良好的json时遇到了一些奇怪的问题。奇怪的是,在我的dev服务器上,它工作正常,但无法在线运行 ajax调用返回的数据如下所示:Javascript 需要帮助解决一个奇怪的json解析问题吗,javascript,jquery,json,jquery-1.4,Javascript,Jquery,Json,Jquery 1.4,我在解析从ajax调用(使用jQuery1.4.4)返回的格式良好的json时遇到了一些奇怪的问题。奇怪的是,在我的dev服务器上,它工作正常,但无法在线运行 ajax调用返回的数据如下所示: returnData = { "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the
returnData = { "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } };
jsLint和jsonLint都验证这个结构
尝试访问returnData.data时出错
在“失败”的情况下,我从jQuery.ajax选项中删除了数据类型,从而允许使用“最佳猜测”功能。如果我指定json,jQuery会抛出一个解析错误,声明json无效。我尝试过各种方法(包括可怕的eval()和jquery-2json插件),但都没有成功,甚至jQ实用工具jquery.parseJSON也失败了
该问题出现在FF 3.6.13和最新的Safari/Chrome中
问题1:为什么有人知道最新的jQuery会在这个问题上抛出解析错误
问题2:当我尝试以下方法时,我成功了:
- var success=returnData.status
- var errorReturn=returnData.data.error\u return
return\u data=
部分,那么它就是无效的json
如果您的ajax调用如下所示:
$.ajax({
url: "your_url",
success: function(data) {
}
});
{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }
success: function(data) {
if (data.status) {
// ...
}
}
…在success
中,如果要访问status
值,您的JSON应该如下所示:
$.ajax({
url: "your_url",
success: function(data) {
}
});
{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }
success: function(data) {
if (data.status) {
// ...
}
}
(注意,开头没有返回_data=
,结尾没有;
。)
…您的success
函数应该如下所示:
$.ajax({
url: "your_url",
success: function(data) {
}
});
{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }
success: function(data) {
if (data.status) {
// ...
}
}
该示例适用于最新的Chrome、Firefox、Opera等浏览器,使用最新的jQuery。可能在某个阶段,您使用的JSON解析器在封面下使用了eval
。您引用的示例是作为赋值语句一部分的有效JavaScript对象文字符号,但不是有效的JSON。一些“JSON”解析器实际上使用eval
来解析JavaScript,而不是JSON,这可能是您的问题。如果您引用的文本实际上是返回的内容,并带有return\u data=
部分,则它是无效的JSON
如果您的ajax调用如下所示:
$.ajax({
url: "your_url",
success: function(data) {
}
});
{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }
success: function(data) {
if (data.status) {
// ...
}
}
…在success
中,如果要访问status
值,您的JSON应该如下所示:
$.ajax({
url: "your_url",
success: function(data) {
}
});
{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }
success: function(data) {
if (data.status) {
// ...
}
}
(注意,开头没有返回_data=
,结尾没有;
。)
…您的success
函数应该如下所示:
$.ajax({
url: "your_url",
success: function(data) {
}
});
{ "status": true, "data": { "error_return": false, "error_index": -1, "message_display": { "main_message": "hello", "name": "tommy tune the man", "mailed_to": "t@t.com", "subject": "I tried this", "subject_message": "you have a technical question or comment.", "test_me": "you have a technical question or comment." } } }
success: function(data) {
if (data.status) {
// ...
}
}
该示例适用于最新的Chrome、Firefox、Opera等浏览器,使用最新的jQuery。可能在某个阶段,您使用的JSON解析器在封面下使用了
eval
。您引用的示例是作为赋值语句一部分的有效JavaScript对象文字符号,但不是有效的JSON。一些“JSON”解析器实际上使用eval
来解析JavaScript而不是JSON,这可能是您的问题。请查看我的更新,因为我已经解决了这个问题。很抱歉在我的原始帖子中没有说得更具体。变量returnedData
实际上在成功回调中(很抱歉没有指定)
为清楚起见,我将以下内容与jQ submit()函数一起使用(在此表单提交的单击处理程序中):
其中,成功回调为:
function
evaluateContactResponse(returnData) {
//and so on }
(旁注:在出现这些故障之前,我一直在使用jQ表单插件,但由于表单插件以静默方式失败,因此我移动到了另一个实现。由于表单插件非常优雅,我现在可以将其移回。)
回调“evaluateResponse”是进行解析的地方,正如我所说的,现在可以工作了
再次感谢…你是个聪明人
N请查看我的更新,因为我已经解决了问题。很抱歉在我的原始帖子中没有说得更具体。变量
returnedData
实际上在成功回调中(很抱歉没有指定)
为清楚起见,我将以下内容与jQ submit()函数一起使用(在此表单提交的单击处理程序中):
其中,成功回调为:
function
evaluateContactResponse(returnData) {
//and so on }
(旁注:在出现这些故障之前,我一直在使用jQ表单插件,但由于表单插件以静默方式失败,因此我移动到了另一个实现。由于表单插件非常优雅,我现在可以将其移回。)
回调“evaluateResponse”是进行解析的地方,正如我所说的,现在可以工作了
再次感谢…你是个聪明人
N实际上,json是一个字符串而不是一个文本,所以它需要是
“{”status:“true”,“data:{”error\u return:“false”,“error\u index:-1,“message\u display:{”main\u message:“hello”,“name:“tommy tune the man”,“mailed\u to:”t@t.com“,”主题“:”我试过这个“,”主题信息“:”你有一个技术问题或意见“,”测试我“:“您有一个技术问题或评论。”}}}
否则它是一个对象文字,甚至是“{\”状态\”:true、\'data\:{\'error\u return\:false、\'error\u index\”:-1、\'message\u display\”:{\'main\u message\“:“hello\”、“name\:“tommy tune the man\”、“mailed\u to\”:”t@t.com\“,”主题“:\”我试过这个“,”主题“\u消息”:\“您有一个技术问题或评论。\”,\“测试我”:\“您有一个技术问题或评论。\”}}}”
这将是大多数服务器端语言生成serialization@Martin:不。JSON不是字符串。它是一种文本数据格式。(我做到了。)