jquery 错误? Safari 中带有对象的 jsonp 不起作用
我正在处理的这个函数遇到“解析错误”。我能够将问题提炼为简单的内容:
function test(){
dataobject={firstname:"John's"};
$.ajax({
url: "http://archive.cyark.org/fieldapp.php",
dataType: "jsonp",
data: {
action:"getprojects1",
dataobject:dataobject
},
success: function(data){
alert("sucess!")
},
error: function(req, status, err){
alert("An error occurred, are you sure you\'re connected to the internet?");
}
});
}
要重现问题,您需要处于 Safari 中。看来正在发送的对象中的单引号有问题。这也是 jsonp 中的一个问题。
所以有两件事,(1)我该怎么做才能解决这个问题?我的对象中可能有一个单引号,我必须考虑到这一点。 (2) 这是 jquery 中的错误吗?
I am getting a "parse error" with this function I'm working on. I was able to distill the problem into just barebones:
function test(){
dataobject={firstname:"John's"};
$.ajax({
url: "http://archive.cyark.org/fieldapp.php",
dataType: "jsonp",
data: {
action:"getprojects1",
dataobject:dataobject
},
success: function(data){
alert("sucess!")
},
error: function(req, status, err){
alert("An error occurred, are you sure you\'re connected to the internet?");
}
});
}
To recreate the problem, you need to be in safari. It seems that the single quote in the object being sent is problematic. And it only is a problem in jsonp as well.
So two things, (1) what do I do to fix this problem? My objects might have a single quote in it and I have to account for that. And (2) is this a bug in jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
olle 对同一问题的回答。
规范规定,在 JSON 中,您只能在键和值周围使用双引号,因此请尝试使用双引号。我很确定您的错误将会得到解决。
您可能希望使用 json.js 对实际值中的特殊字符进行编码/转义,这样您就不会遇到包含“的值的问题。例如,或者来自 http://www.json.org/js.html
olle's answer for the same issue.
the specs say that in JSON you can only use double-quotes around keys and values so try it with double quotes. I am pretty sure your error will be solved.
you may want to use json.js to encode / escape special chars in the actual values so you don't run into problems with values containing " for instance. or the stringify method from http://www.json.org/js.html