jQuery ajax成功回调不会触发。

时间:2022-05-12 19:55:13

I'm using jQuery's $.ajax method to fetch data from a JSONP-compatible web service and as far as getting a response and displaying the received data in my HTML, everything seems to be working fine. However, I haven't been able to get the ajax method's success callback to fire. What's even stranger is that even when I do get a valid response from the service, the error callback is always fired.

我使用jQuery的美元。ajax方法从jsonp兼容的web服务中获取数据,并在我的HTML中得到响应和显示接收到的数据,一切看起来都很正常。但是,我还没有得到ajax方法的成功回调。更奇怪的是,即使我从服务获得了有效的响应,错误回调总是被触发。

Here is an example of a simple js function I made to test this:

这里有一个简单的js函数的例子,我做了一个测试:

function doJsonp()
{
    $.ajax({
        url: "http://example.com/api/service?callback=blah",
        dataType: "jsonp",
        crossDomain: true,
        success: function() { console.log("success"); }, // This is never fired
        error: function() { console.log("error"); } // This is always fired
    });
}

and the matching callback function:

和匹配回调函数:

function blah(data)
{
    console.log(data); // This is called properly
}

From reading similar questions here on SO and elsewhere, it seems that this is most often caused by the service returning JSON that does not validate. For my purposes, I am using an in-house web service but have tried other JSONP services as well, such as those provided by Flickr, for example:

从在这里和其他地方阅读类似的问题,似乎这通常是由返回的JSON导致的,而这个JSON并没有验证。出于我的目的,我使用的是内部web服务,但也尝试了其他JSONP服务,例如Flickr提供的服务,例如:

http://api.flickr.com/services/feeds/groups_pool.gne?id=807213@N20&lang=en-us&format=json&jsoncallback=blah

http://api.flickr.com/services/feeds/groups_pool.gne?id=807213@N20&lang = en-us&format = json&jsoncallback =

Both the JSON from Flickr's service and mine validate using jsonlint so that does not seem to be the issue as far as I can tell.

从Flickr的服务和我的服务的JSON都使用jsonlint来验证,这似乎并不是我所能看到的问题。

In searching for solutions to this problem, I have tried using a jQuery plugin called jquery-jsonp, found at http://code.google.com/p/jquery-jsonp/. This replaces jQuery's $.ajax call with its own $.jsonp so the above code looks like this:

在寻找这个问题的解决方案时,我尝试使用一个jQuery插件jQuery -jsonp,在http://code.google.com/p/jquery-jsonp/上找到。这替换jQuery的美元。ajax调用自己的$。jsonp上面的代码是这样的:

function doJsonp()
{
    $.jsonp({
        url: "http://example.com/api/service?callback=blah",
        success: function() { console.log("success"); },
        error: function() { console.log("error"); }
    });
}

However, the result is the same and the success callback never fires. Any help or a nudge in the right direction would be greatly appreciated!

然而,结果是相同的,成功回调从不触发。如果有任何帮助或推动正确的方向,将非常感谢!

1 个解决方案

#1


4  

define the callback-function via the jsonpCallback-option, not inside the url:

通过jsonpcallback选项定义callback函数,而不是在url中:

function doJsonp()
{
    $.ajax({
        url: "http://api.flickr.com/services/feeds/groups_pool.gne?id=807213@N20&lang=en-us&format=json&jsoncallback=?",
        dataType: "jsonp",
        crossDomain: true,
        jsonpCallback:'blah',//<<<
        success: function() { console.log("success"); }, 
        error: function() { console.log("error"); } 
    });
}

#1


4  

define the callback-function via the jsonpCallback-option, not inside the url:

通过jsonpcallback选项定义callback函数,而不是在url中:

function doJsonp()
{
    $.ajax({
        url: "http://api.flickr.com/services/feeds/groups_pool.gne?id=807213@N20&lang=en-us&format=json&jsoncallback=?",
        dataType: "jsonp",
        crossDomain: true,
        jsonpCallback:'blah',//<<<
        success: function() { console.log("success"); }, 
        error: function() { console.log("error"); } 
    });
}