设置超时AJAX(jQuery的) [英] Set timeout for ajax (jQuery)

查看:142
本文介绍了设置超时AJAX(jQuery的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.ajax({
    url: "test.html",
    error: function(){
        //do something
    },
    success: function(){
        //do something
    }
});

有时候成功函数工作好,有时没有。

Sometimes success function works good, sometimes not.

我如何设置超时这个ajax请求?在例如,3秒,如果时间超出,则显示错误。

How do I set timeout for this ajax request? In example, 3 seconds, if time is out, then show an error.

现在的问题是,Ajax请求冻结块,直到完成。如果服务器是下来了一点时间,它永远不会结束。

The problem is, ajax request freezes the block until finishes. If server is down for a little time, it will never end.

推荐答案

请阅读 $。阿贾克斯documentation ,这是一个有盖的话题。

Please read the $.ajax documentation, this is a covered topic.

$.ajax({
    url: "test.html",
    error: function(){
        // will fire when timeout is reached
    },
    success: function(){
        //do something
    },
    timeout: 3000 // sets timeout to 3 seconds
});

您可以看到什么类型的错误被抛出通过访问错误textStatus参数:功能(jqXHR,textStatus,errorThrown)选项。选项​​有超时,错误,中止和parsererror。

You can get see what type of error was thrown by accessing the textStatus parameter of the error: function(jqXHR, textStatus, errorThrown) option. The options are "timeout", "error", "abort", and "parsererror".

这篇关于设置超时AJAX(jQuery的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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