简体   繁体   English

为什么从我的jQuery.getJSON()方法中调用fail方法?

[英]Why is the fail method being called from my jQuery.getJSON() method?

I have a html page loaded directly from a local file in a web browser (FF 14.0.1 or Chromium, on Ubuntu 12.04) 我有一个直接从Web浏览器中的本地文件加载的html页面(FF 14.0.1或Chromium,在Ubuntu 12.04上)

The html page includes a local jQuery js file, and then includes a local js file with this method: html页面包含一个本地jQuery js文件,然后包含带有此方法的本地js文件:

function start() {
     $.getJSON("http://localhost:8080/app/connect?callback=?", "id=11", function() { alert("win!"); })
     .done(function() { alert("done"); })
     .fail(function(xhr, request, error) { alert(xhr.status + "<> + request + "<>" + error); }); }

I'm responding to these requests from a JBoss / Restful web server with no special configuration, the method on the server is annotated with @Produces({"application/json"}). 我正在响应来自JBoss / Restful Web服务器的这些请求,没有特殊配置,服务器上的方法使用@Produces({“ application / json”})进行注释。 Server doesn't show any errors, even gets the id value correctly. 服务器不显示任何错误,甚至正确获取id值。

When I trigger this javascript, the fail method is called and I get this alert: 当我触发此javascript时,将调用fail方法,并收到以下警报:

200<>parsererror<>jQuery311391951_513134 was not called

I can see the JSON response when inspecting with Firebug, it looks ok. 使用Firebug进行检查时,我可以看到JSON响应,看起来还可以。 In Chromium, I can inspect the request / response headers and everything looks ok. 在Chromium中,我可以检查请求/响应头,一切看起来都很好。 I have that text saved, if anyone thinks it might give more insight. 我保存了该文本,如果有人认为它可以提供更多的见解。

Can anyone tell me what's going on? 谁能告诉我怎么回事? Why do I get this error? 为什么会出现此错误?

Thanks in advance! 提前致谢!

It looks like you are accessing a local resource, in that case you don't have to use jsonp. 看起来您正在访问本地资源,在这种情况下,您不必使用jsonp。 You can remove the callback=? 您可以删除callback=? from the url. 来自网址。

It is used if you have to access a third party resource with Same Origin Policy violations 如果您必须访问违反同源策略的第三方资源,则使用它

function start() {
    $.getJSON("http://localhost:8080/app/connect", "id=11", function() { 
        alert("win!");
    }).done(function() { 
        alert("done"); 
    }).fail(function(xhr, request, error) { 
        alert(xhr.status + "<>" + request + "<>" + error); 
    }); 
}

You are using JSONP (JSON with Padding). 您正在使用JSONP(带有填充的JSON)。 The server must unterstand the ?callback= Paramater and wrap the JSON answer with the callback function. 服务器必须理解?callback=参数,并使用回调函数包装JSON答案。 This is mandatory for calls where SOP restrictions apply. 这适用于有SOP限制的呼叫。 If not (same domain and port) then remove the callback parameter. 如果不是(相同的域和端口),则删除回调参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM