首先,我一直在寻找一些话题回答我的问题,我无法找到一个解决方案,我的code工作。
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.
我想从一个servlet得到答案,如果我去的http:// XXXZZZ / Servlet的/登录密码=佩佩和放大器;通过= 1234
我收到如预期有效的JSON:
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:
{"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"}
但是当我尝试在同$阿贾克斯,我得到2个错误。
But when I try the same with $.ajax, I get 2 errors.
$.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
二错误(在Chrome控制台):
Second error (in the Chrome console):
Uncaught SyntaxError: Unexpected token : Login 1
(还有就是我在等待的JSON)。
(And there is the JSON I'm waiting for).
P.S。我必须使用数据类型:JSONP,因为如果我使用JSON我也有问题,跨域
P.S. I have to use dataType : "jsonp", because if I use "json" I also have problems with the Cross-Domain.
如果您正在使用JSONP那么语法错
If you are using jsonp then the syntax is wrong
您需要返回
myJsonMethod({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"});
和也添加到您的Ajax请求的选项
and also add to your ajax request options
jsonp: false,
jsonpCallback: "myJsonMethod"
所以
$.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);
}
});
(和道修复成功
的 @voyager 注意到的)
(and of-course fix the success
as @voyager noted)