当jQuery ajax超时时,我想运行一个函数 [英] when there is timeout at jQuery ajax, I want to run a function

查看:163
本文介绍了当jQuery ajax超时时,我想运行一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jquery $ .ajax超时设置

Possible Duplicate:
jquery $.ajax timeout setting

超时有效,但是发生超时时,我想执行一个函数.你有什么想法怎么做吗?

The timeout works, but when the timeout occurs, I want to execute a function. do you have any ideas how to do this?

$.ajax({
    type: "GET", 
    url: "xajax.php", 
    timeout: 100, 
    data: "name=John&location=Boston", 
    success: function(msg) {
        alert( "Data Saved: " + msg );
    }
});

推荐答案

使用完整:

当请求完成时(成功执行后, 错误回调).该函数将传递两个参数: jqXHR对象(在jQuery 1.4.x中,XMLHTTPRequest)和一个字符串 对请求的状态进行分类(成功",未修改", 错误",超时" ,中止"或"parsererror").从jQuery 1.5开始, 完整的设置可以接受一系列功能.每个功能将 依次被调用.这是一个Ajax事件.

A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

或错误:

如果请求失败,将调用的函数.功能接收 三个参数:jqXHR对象(在jQuery 1.4.x中,XMLHttpRequest),一个 描述发生的错误类型的字符串和一个可选的 异常对象(如果发生).第二个可能的值 参数(除null外)为超时" ,错误",中止"和 "parsererror".发生HTTP错误时,errorThrown会收到 HTTP状态的文本部分,例如未找到"或内部 服务器错误."从jQuery 1.5开始,错误设置可以接受一个数组 功能.每个函数将依次调用.注意:此处理程序 跨域脚本和JSONP请求未调用.这是一 Ajax事件.

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.

以下是使用complete的示例:

Here is an example using complete:

    $.ajax({ 
    type: "GET", 
    url: "xajax.php", 
    timeout: 100, 
    data: "name=John&location=Boston", 
    success: function(msg){ alert( "Data Saved: " + msg ); },
    complete: function(jqXHR, textStatus) { 
        if (textStatus == "timeout") {
           alert('timeout');
        }
    }
    });

这篇关于当jQuery ajax超时时,我想运行一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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