使用ajax检测HTTP错误

发布于 2024-12-28 08:51:04 字数 734 浏览 5 评论 0原文

我正在使用 ajax 调用来检索一些 HTML 数据。在某些情况下,我可能会收到 HTTP 错误 403(由于某些 IE bug 在 IE 上使用 ajax 调用时遇到 403 错误, 带有正斜杠抛出 403 的哈希片段IE 中的 AJAX 请求错误)。

出于错误处理的目的,我想捕获发生的错误并相应地显示一些错误消息。

我该怎么做?

也许像

$.ajax({
    type : 'POST',
    data : data,
    cache : false,
    url : url,
    dataType : 'html'
    success : function(){
        //if HTTP response is ok, diplay data
        //else if HTTP response gets 403 error, display some other message
    }
});

I'm using ajax calls to retrieve some HTML data. In certain cases, there are chances that I may get HTTP error 403 (because of some IE bug Encountering 403 error while using ajax call on IE, Hash fragments with forward-slash throwing 403 errors with AJAX requests in IE).

For error handling purposes, I want to catch what error has occured and accordingly display some error message.

How do I do that?

Maybe something like

$.ajax({
    type : 'POST',
    data : data,
    cache : false,
    url : url,
    dataType : 'html'
    success : function(){
        //if HTTP response is ok, diplay data
        //else if HTTP response gets 403 error, display some other message
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2025-01-04 08:51:04

引用文档

错误(jqXHR,textStatus,错误抛出)
功能
请求失败时调用的函数。
该函数接收三个参数:jqXHR(在 jQuery 1.4.x 中为 XMLHttpRequest)对象、描述发生的错误类型的字符串以及可选的异常对象(如果发生)。第二个参数的可能值(除了 null 之外)有“timeout”、“error”、“abort”和“parsererror”。当发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。从 jQuery 1.5 开始,错误设置可以接受函数数组。每个函数都会被依次调用。注意:跨域脚本和 JSONP 请求不会调用此处理程序。

$.ajax({
    type : 'POST',
    data : data,
    cache : false,
    url : url,
    dataType : 'html'
    success : function(){
        //if HTTP response is ok, diplay data
        //else if HTTP response gets 403 error, display some other message
    },
    error: function(){
        // Handle your error here
    }
});

To quote the documentation:

error(jqXHR, textStatus, errorThrown)
Function
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.

$.ajax({
    type : 'POST',
    data : data,
    cache : false,
    url : url,
    dataType : 'html'
    success : function(){
        //if HTTP response is ok, diplay data
        //else if HTTP response gets 403 error, display some other message
    },
    error: function(){
        // Handle your error here
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文