使用jQuery AJAX捕获404状态 [英] Capture 404 status with jQuery AJAX

查看:1595
本文介绍了使用jQuery AJAX捕获404状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        alert(ajaxContext.responseText)
    }
});

对于ajaxContext以及如何捕获404返回代码,我仍然感到困惑.但是我还有另一个问题.我正在阅读有关成功与失败进行编码的知识,并且在最新版本的jQuery中不再使用错误.

I am still confused about the ajaxContext and how to capture 404 return codes. However I have another question. I am reading something about coding with success and fail and no longer using error in the recent versions of jQuery.

所以我应该更改代码以使用完成和失败.那我怎样才能检查404?

So should I change my code to use done and fail. How then could I check for a 404?

推荐答案

按如下所示替换错误函数...

Replace your error function as follows...

error:function (xhr, ajaxOptions, thrownError){
    if(xhr.status==404) {
        alert(thrownError);
    }
}

这篇关于使用jQuery AJAX捕获404状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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