jQuery 为什么这是无效的 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5052010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this invalid JSON?
提问by oompahloompah
I am sending some data from an server back to a page. It looks like valid JSON to me, yet, it is being caught/handled by the error handler in my jQuery.ajax() call.
我正在将一些数据从服务器发送回页面。对我来说它看起来像是有效的 JSON,但是,它被我的 jQuery.ajax() 调用中的错误处理程序捕获/处理。
This is the error message displayed:
这是显示的错误消息:
Error: Invalid JSON: ({"id":"#settingsResult","payload":"<form id=\"pwd_change_frm\" action=\"post\">\n <div>\n <div>\n <div><\/div>\n <div><label for=\"changepwd_password\">New Password<\/label><\/div>\n <div><input type=\"password\" name=\"changepwd[password]\" title=\"Enter new password\" style=\"width:258px;\" id=\"changepwd_password\" \/><\/div>\n <\/div>\n <div class=\"spacer\"> <\/div>\n <div>\n <div><\/div>\n <div><label for=\"changepwd_password_confirm\">Confirm New Password<\/label><\/div>\n <div><input type=\"password\" name=\"changepwd[password_confirm]\" title=\"Retype new password)\" style=\"width:258px;\" id=\"changepwd_password_confirm\" \/><\/div>\n <\/div>\n <div class=\"spacer\"><\/div>\n <div><img id=\"pwd_chg_btn\" class=\"submit_btn\" src=\"\/images\/button_submit.gif\" alt=\"Button_submit\" \/><\/div>\n <\/div>\n <input type=\"hidden\" name=\"changepwd[_csrf_token]\" value=\"1b7f3529797245c0fc43c3ddf5ade30d\" id=\"changepwd__csrf_token\" \/><\/form>\n<div class=\"spacer\"><\/div>"})
As an aside, FF Firebug correctly parses the returned data and displays ot correctly - which is why I dont understand why jQuery can't seem to handle it.
顺便说一句,FF Firebug 正确解析返回的数据并正确显示 - 这就是为什么我不明白为什么 jQuery 似乎无法处理它。
here is the bit of code that makes the call:
这是进行调用的代码:
jQuery.ajax({
type: 'POST',
url: '/some_url?id='+this.id,
timeout: 2000,
success: function(result){ jQuery(result.id).html(result.payload); },
error: function (xhr, ajaxOptions, thrownError){ alert('Error: '+ thrownError); }
});
回答by user113716
Probably useless to answer at this point, but I just thought I'd note that the issue does somehow seem to be with $.parseJSON()
.
在这一点上回答可能没用,但我只是想我会注意到这个问题似乎与$.parseJSON()
.
If I remove all occurrences of \n
, and replace all occurrences of \"
with \'
, it works.
如果我删除的所有实例\n
,并更换所有出现\"
用\'
,它的工作原理。
Alternatively, if I double escape them, it works.
或者,如果我双重逃避它们,它会起作用。
\n
\"
...which would make sense since I believe jQuery simply does an eval()
(or effectively the same thing), so I guess escaping the "
is terminating the string and escaping the \n
is introducing a newline character where it would be invalid.
...这是有道理的,因为我相信 jQuery 只是做了一个eval()
(或实际上相同的事情),所以我猜转义"
是终止字符串,转义\n
是引入一个无效的换行符。
回答by ntziolis
Because of the surrounding '()', remove it and its valid json.
由于周围的 '()',删除它和它的有效 json。
回答by Quentin
Since everything else appears to be valid, the problem is likely that you are including (
and )
in the data.
由于其他所有内容似乎都是有效的,因此问题很可能是您包含(
并包含)
在数据中。
回答by Adrian B.
The error also appears when you use jquery validate (version 1.7). You can check: http://datatables.net/forums/comments.php?DiscussionID=3993&page=1#Item_16
使用 jquery validate(1.7 版)时也会出现该错误。您可以查看:http: //datatables.net/forums/comments.php?DiscussionID=3993&page=1#Item_16
I also upgraded to jquery 1.5.1 and i had the same issue. After i updated jquery validate (from here: https://github.com/jzaefferer/jquery-validation) the error disappeared.
我也升级到 jquery 1.5.1,我遇到了同样的问题。在我更新 jquery 验证(从这里:https: //github.com/jzaefferer/jquery-validation)后,错误消失了。
回答by D.Shawley
What is the actual JSON that is being passed? The string in the error message:
传递的实际 JSON 是什么?错误消息中的字符串:
{"id":"#settingsResult",
"payload":"\n \n \n <\/div>\n New Password<\/label><\/div>\n <\/div>\n <\/div>\n <\/div>\n \n <\/div>\n Confirm New Password<\/label><\/div>\n <\/div>\n <\/div>\n <\/div>\n <\/div>\n <\/div>\n <\/form>\n<\/div>"}
is valid according to JSONLint. Are you including the parens?
根据JSONLint是有效的。你包括括号吗?
Update after Question Changed
问题更改后更新
Is it possible that <\/div>
is somehow confusing things? This isn't valid HTML so I don't know what jQuery(result.id).html(result.payload)
is going to do with it.
是否有可能<\/div>
以某种方式使事情变得混乱?这不是有效的 HTML,所以我不知道jQuery(result.id).html(result.payload)
将如何处理它。
回答by Nat
jquery requires that you supply "dataType: json" unless you supply the proper content type in your serverside code like "header('Content-type: application/json');"
jquery 要求您提供“dataType: json”,除非您在服务器端代码中提供正确的内容类型,例如“header('Content-type: application/json');”
回答by Kasun Gunathilake
According to json.org
根据 json.org
JSON can be either an array or an object
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with }
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ]
JSON 可以是数组或对象
对象是一组无序的名称/值对。对象以 {(左大括号)开头,以 } 结尾
数组是值的有序集合。数组以 [(左括号)开始并以 ] 结束
So JSON should start with either '{' or '[' and it can't start with '('
所以 JSON 应该以 '{' 或 '[' 开头,不能以 '('
Also you use following online validator like debugjson.com to always check errors of your JSON input.
您还可以使用以下在线验证器(如 debugjson.com)来始终检查 JSON 输入的错误。