$就和JSONP。 ParseError和未捕获的SyntaxError:意外的标记: [英] $.ajax and JSONP. ParseError and Uncaught SyntaxError: Unexpected token :

查看:413
本文介绍了$就和JSONP。 ParseError和未捕获的SyntaxError:意外的标记:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我一直在寻找一些话题回答我的问题,我无法找到一个解决方案,我的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)

这篇关于$就和JSONP。 ParseError和未捕获的SyntaxError:意外的标记:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆