0

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

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

我怎么做?

也许像

$.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
    }
});
4

1 回答 1

1

引用文档

error(jqXHR, textStatus, errorThrown)
函数
请求失败时调用的函数。
该函数接收三个参数: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
    }
});
于 2012-01-21T15:03:24.217 回答