Ext Js(4) - AJAX - 超时时的回调函数?

时间:2015-07-22 10:59:51

标签: extjs

我希望在AJAX请求超时时调用一个函数,例如:

Ext.override(Ext.data.proxy.Ajax, {
    timeout: 60000,
    onTimeout: function () {
        alert("Sorry, the request timed out. Please try again.");
    }
});

假设超时将被视为错误/失败,我也尝试过:

Ext.override(Ext.data.proxy.Ajax, {
    timeout: 60000,
    failure: function () {
        alert("Sorry, the request timed out. Please try again.");
    }
});

但似乎failure仅在返回HTTP状态代码时触发(并且不等于200)。

我真的在互联网上寻找解决方案,但似乎没有onTimeoutonCancel

对此有何建议,我如何实现这样的全局功能?

1 个答案:

答案 0 :(得分:2)

使用exception侦听器并检查请求状态是否等于0:

<div class="content">
        @*@using (Ajax.BeginForm("AccountCheckLogin2", "Login", null, new AjaxOptions { OnFailure = "OnFailure", OnSuccess = "OnSuccess", UpdateTargetId = "result" }, new { @name = "form", role = "form" }))*@
        @using (Ajax.BeginForm("AccountCheckLogin2", "Login", new AjaxOptions { OnSuccess = "OnSuccessLogin", OnBegin = "OnBeginLogin", OnComplete = "OnCompleteLogin", OnFailure = "OnFailureLogin" }))
        {
            //inputs here
        }
           <a class="link" href="/Registration/Registration">Я не зареестрований</a>
        </div>
</div>
<script type="text/javascript">       
    function OnSuccessLogin(response) {
        if (response.ResponseCode == "1") {
            window.location = "/";
        }
        else
        {                
            $("#LoginValidationSummary ul").append("<li>"+response.ResponseMessage+"</li>");
        }
    }
    function OnBeginLogin() {
        $("#loginSubmit").prop("disabled", true);
    }
    function OnCompleteLogin() {
        $("#loginSubmit").prop("disabled", false);
    }
    function OnFailure() {
        alert("Whoops! That didn't go so well did it?");
    }

</script>

示例:https://fiddle.sencha.com/#fiddle/qt7