jQuery $.ajax 和 JSONP。ParseError 和 Uncaught SyntaxError: Unexpected token :
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10507345/
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
$.ajax and JSONP. ParseError and Uncaught SyntaxError: Unexpected token :
提问by Pablo Postigo
First of all, I've been looking for the answer to my problem in several topics and I couldn't find a solution that works with my code.
首先,我一直在几个主题中寻找我的问题的答案,但找不到适用于我的代码的解决方案。
I'm trying to get the answer from a servlet, if I go to http://XXXZZZ/Servlet/Login?login=pepe&pass=1234
I receive valid JSON as expected:
我正在尝试从 servlet 中获取答案,如果我去,http://XXXZZZ/Servlet/Login?login=pepe&pass=1234
我会按预期收到有效的 JSON:
{"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"}
But when I try the same with $.ajax, I get 2 errors.
但是当我用 $.ajax 尝试同样的方法时,我得到 2 个错误。
$.ajax({
type : "Get",
url :"http://XXXZZZ/Servlet/Login",
data :"login="+login+"&password="+pass,
dataType :"jsonp",
success : function(data){
alert(data);},
error : function(httpReq,status,exception){
alert(status+" "+exception);
}
});
First error (in the popup window):
第一个错误(在弹出窗口中):
parsererror Error: jQuery17104145435250829905_1336514329291 was not called
Second error (in the Chrome console):
第二个错误(在 Chrome 控制台中):
Uncaught SyntaxError: Unexpected token : Login 1
(And there is the JSON I'm waiting for).
(还有我正在等待的 JSON)。
P.S. I have to use dataType : "jsonp", because if I use "json" I also have problems with the Cross-Domain.
PS我必须使用dataType:“jsonp”,因为如果我使用“json”,我也会遇到跨域问题。
回答by Gabriele Petrioli
If you are using jsonp then the syntax is wrong
如果您使用的是 jsonp 那么语法是错误的
You need to return
你需要返回
myJsonMethod({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"});
and also add to your ajax request options
并添加到您的 ajax 请求选项
jsonp: false,
jsonpCallback: "myJsonMethod"
so
所以
$.ajax({
type : "Get",
url :"http://XXXZZZ/Servlet/Login",
data :"login="+login+"&password="+pass,
dataType :"jsonp",
jsonp: false,
jsonpCallback: "myJsonMethod",
success : function(data){
alert(data);},
error : function(httpReq,status,exception){
alert(status+" "+exception);
}
});
(and of-course fix the success
as @voyagernoted)
(当然,success
正如@voyager指出的那样修复)
回答by Esteban Küber
succes : function(data){
That's a typo:
这是一个错字:
success : function(data){
回答by Shawn Lehner
First off you have a typo in your success parameter; you missed the ending s. Also, when you are performing a JSONP request you need to return your JSON information in JSONP format; which should include the callback token as part of the return string. A sample JSONP string would look likes this:
首先,您的成功参数中有一个错字;你错过了结尾 s。此外,当您执行 JSONP 请求时,您需要以 JSONP 格式返回您的 JSON 信息;它应该包括回调标记作为返回字符串的一部分。示例 JSONP 字符串如下所示:
yourcallbacktoken({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"})
Take a look at this page for more information on the JSONP specifications: http://devlog.info/2010/03/10/cross-domain-ajax/
有关 JSONP 规范的更多信息,请查看此页面:http: //devlog.info/2010/03/10/cross-domain-ajax/