javascript 语法错误:缺少;语句之前 jquery jsonp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20658674/
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
SyntaxError: missing ; before statement jquery jsonp
提问by Ranveer Singh Rajpurohit
I am using below code to access rest service hosted on another domain.
我正在使用以下代码访问托管在另一个域上的休息服务。
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType:"jsonp",
success: function(json) {
alert(json);
},
error: function(e) {
console.log(e.message);
}
});
I am able to get the data correctly, but I get this error in firebug in mozilla:
我能够正确获取数据,但是在 mozilla 的 firebug 中出现此错误:
SyntaxError: missing ; before statement
{"Hello":"World"}
语法错误:缺少;声明前
{“你好世界”}
Can anyone suggest me what I am doing wrong here? Even though Json data is valid. I tried all the suggestions posted in this questionBut still I am getting same error.
谁能告诉我我在这里做错了什么?即使 Json 数据有效。我尝试了此问题中发布的所有建议但仍然遇到相同的错误。
回答by Denys Séguret
If it's really JSON you ask for, don't set "jsonp"
as dataType
, and don't provide a callback :
如果它确实是您要求的 JSON,请不要设置"jsonp"
为dataType
,并且不要提供回调:
$.ajax({
type: 'GET',
url: url,
contentType: "application/json",
success: function(json) {
alert(json);
},
error: function(e) {
console.log(e.message);
}
});
回答by Rune FS
the format of JSON and JSONP are slight?y different JKSONP is a function invocation expression
JSON 和 JSONP 的格式略有不同 JKSONP 是一个函数调用表达式
callback({"hellow":"world"});
whereas JSON is simply a serialized object
而 JSON 只是一个序列化的对象
{"Hello":"world"}
fromyour posting it would seem that the server is returning JSON and not JSONP
从您的帖子看来,服务器正在返回 JSON 而不是 JSONP
So you either need to change the server to reply correctly (The actual callback name is a get parameter to the request). You should do this if you are using the ajax call across domains
因此,您要么需要更改服务器以正确回复(实际的回调名称是请求的 get 参数)。如果您在跨域使用 ajax 调用,则应该这样做
If you are not using ajax across domains stick to regular JSON
如果您不跨域使用 ajax,请坚持使用常规 JSON