jQuery.ajax - 总是()并不总是运行 [英] jQuery.ajax - always() not always running

查看:95
本文介绍了jQuery.ajax - 总是()并不总是运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery.ajax做一个REST调用和检索某些JSON。它工作正常。然而,当我强迫错误情况,如无效的URL时,总是方法不火。如果我设置跨域=虚假或数据类型='json的',然后一直不火。但是,我不能这样做,在我的生产code。如果设置URL =' http://ip.jsontest.com/ ,然后始终闪光。我创建了一个小例子来说明这个问题:

I'm using jQuery.ajax to make a REST call and retrieve some JSON. It's working as expected. However, when I force an error condition, such as an invalid URL, the always method does not fire. If I set crossDomain=false or dataType='json', then always does fire. But, I can't do that in my production code. If you set url='http://ip.jsontest.com/' then always fires. I created a small example to illustrate the problem:

var jqXHR = jQuery.ajax({
    type: 'GET',
    async: false,
    cache: false,
    url: 'http://ip.jsontest.com.BADURL/',
    contentType: 'application/json; charset=UTF-8',
    crossDomain: true,
    dataType: 'jsonp'
})
.done(function (data, textStatus, jqXHR) {
    console.log('Your IP is ' + data.ip);
    console.log('done was called');
})
.fail(function (jqXHR, textStatus, errorThrown) {
    console.log('fail was called');
})
.always(function (dataOrjqXHR, textStatus, jqXHRorErrorThrown) { console.log('always was called'); });

您可以在jquery.com控制台是使用jQuery 1.9.1运行此。我使用jQuery 1.11.1相同的行为。我总是需要火来处理时候的URL不可用。我得到了IE11,Chrome浏览器38和FF 33.我是否相同的行为做得不对,或这是一个错误? 谢谢你。

You can run this in the console at jquery.com which is using jQuery 1.9.1. I have the same behavior using jQuery 1.11.1. I need always to fire to handle times when the url is unavailable. I get the same behavior in IE11, Chrome 38 and FF 33. Am I doing something wrong or is this a bug? Thanks.

推荐答案

这是著名的JSONP调用的东西。按照 $就参考错误

This is something that is known for JSONP calls. According to the $.ajax reference for error:

注:此处理程序不叫跨域脚本和跨域JSONP请求

Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

另外请注意,不支持同步JSONP调用:

Also note that synchronous JSONP calls are not supported:

跨域请求和数据类型:JSONP请求不支持同步操作

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.

解决方法通常包括两种1)设置超时通话或2)使用插件来添加更多的典型错误的功能。

Workarounds typically involve either 1) setting a timeout for the call or 2) using a plugin to add more typical error functionality.

1)设置超时时间(和异步真)

1) Setting a timeout (and async true)

var jqXHR = jQuery.ajax({
    type: 'GET',
    async: true,
    cache: false,
    url: 'http://ip.jsontest.com.BADURL/',
    contentType: 'application/json; charset=UTF-8',
    crossDomain: true,
    dataType: 'jsonp',
    timeout: 2000
})
.fail(function (jqXHR, textStatus, errorThrown) {
    console.log('fail was called');
})
.done(function (data, textStatus, jqXHR) {
    console.log('Your IP is ' + data.ip);
    console.log('done was called');
})
.always(function (dataOrjqXHR, textStatus, jqXHRorErrorThrown) { console.log('always was called'); });

2) jQuery的JSONP插件它增加了差错恢复功能。

2) The jQuery JSONP plugin which adds error recovery features.

这篇关于jQuery.ajax - 总是()并不总是运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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