语法错误:使用jQuery.ajax无效标签()语法错误、标签、jQuery、ajax

2023-09-10 19:29:22 作者:浪痞

我试图用使一个Ajax调用我的服务器下面的的jQuery 的电话:

  $。阿贾克斯({
    键入:GET,
    数据类型:JSONP
    网址:http://iceworld.sls-atl.com/api/&cmd=time
    成功:功能(数据){
        的console.log(成功);
        的console.log(数据);
    },
    错误:功能(错误){
        的console.log(错误);
        执行console.log(错误);
    },
});
 

我得到了我从浏览器所需的数据,但萤火虫口口声声说语法错误:无效的标签,如下图所示:

那么,是什么让我为难,所以错误回调被调用,而不是成功的原因。我不知道我做错了什么在这里。

解决方案

JSONP数据必须以格式返回:回调(的JSONObject)。这就是为什么你得到一个无效的标签错误。它是期待一个函数,而不是一个JSON对象。您需要修改你的服务器code与回调函数的名称,包装的返回值。当你要求JSONP的名字会被自动添加到请求的jQuery。如果你看的要求,你应该看到这样的事情:

http://iceworld.sls-atl.com/api/&cmd=time?callback=jQuery191035087670385837555_1365126604422&_=1365126604423

您的脚本需要采取回调参数,并用它来包装数据,所以在这个例子中它是这样的:

  jQuery191035087670385837555_1365126604422({地位:1,数据:1365126534})
 
jquery ajax 报错 java.lang.ClassNotFoundException service.LianJie

如果您是从同一产地访问服务器,你可以简单地使用JSON代替。

I'm trying to make an Ajax call to my server using the following jQuery call:

$.ajax({
    type: "GET",
    dataType: "jsonp",
    url: "http://iceworld.sls-atl.com/api/&cmd=time",
    success: function (data) {
        console.log("success");
        console.log(data);
    },
    error: function (error) {
        console.log("error");
        console.log(error);
    },
});

I get the data that I expect from the browser, but Firebug keeps saying "SyntaxError: invalid label" as shown below:

So, what puzzles me is the reason why the error callback is called instead of success. I'm wondering what I did wrong here.

解决方案

JSONP data must be returned in the format: callback( jsonObject ). That is why you are getting an invalid label error. It is expecting a function, not a JSON object. You need to modify your server code to wrap the return value with the name of the callback function. The name is automatically added to the request by jQuery when you request JSONP. If you watch the request, you should see something like this:

http://iceworld.sls-atl.com/api/&cmd=time?callback=jQuery191035087670385837555_1365126604422&_=1365126604423

Your script needs to take the callback parameter and use that to wrap the data, so for this example it would look like this:

jQuery191035087670385837555_1365126604422({"status":1,"data":"1365126534"})

If you are accessing the server from the same origin, you could simply use JSON instead.