jQuery ajax 错误函数 [英] jQuery ajax error function

查看:23
本文介绍了jQuery ajax 错误函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ajax 调用将数据传递到一个页面,然后返回一个值.

我已经从页面中检索到成功的调用,但我已经对其进行了编码,因此它会在 asp.xml 中引发错误.我如何从 jquery 中检索该错误?

例如:

缓存:假,url: "addInterview_Code.asp",类型:POST",数据类型:文本",数据:strData,成功:功能(html){alert('成功:' + html);$("#result").html("成功");},错误:函数(错误){**警报('错误;' + eval(错误));**}

这是我不明白的错误位.在函数中我需要放置什么参数,以便我可以使用我在服务器中提出的错误消息.

解决方案

Ajax error 函数中需要的参数是 jqXHR, exception 可以像下面这样使用:

$.ajax({网址:'some_unknown_page.html',成功:功能(响应){$('#post').html(response.responseText);},错误:函数(jqXHR,异常){var msg = '';如果(jqXHR.status === 0){msg = '未连接.
 验证网络.';} else if (jqXHR.status == 404) {msg = '找不到请求的页面.[404]';} else if (jqXHR.status == 500) {msg = '内部服务器错误 [500].';} else if (exception === 'parsererror') {msg = '请求的 JSON 解析失败.';} else if (异常 === '超时') {msg = '超时错误.';} else if (异常 === '中止') {msg = 'Ajax 请求中止.';} 别的 {msg = '未捕获的错误.
' + jqXHR.responseText;}$('#post').html(msg);},});

您也可以在自己的浏览器控制台中查看此内容,方法是在 error 函数中使用 console.log,例如:

错误:函数(jqXHR,异常){控制台日志(jqXHR);//你的错误处理逻辑在这里..}

我们使用这个对象的 status 属性来获取错误代码,就像我们得到 status = 404 这意味着无法找到请求的页面.它根本不存在.根据该状态代码,我们可以将用户重定向到登录页面或我们业务逻辑所需的任何内容.

例外:

这是显示异常类型的字符串变量.因此,如果我们收到 404 错误,exception 文本将只是错误".同样,我们可能会得到 'timeout'、'abort' 作为其他异常文本.

<小时><块引用>

弃用通知:jqXHR.success()jqXHR.error()jqXHR.complete() 回调从 jQuery 1.8 开始被弃用.准备您最终删除的代码,请使用 jqXHR.done(), jqXHR.fail(),和 jqXHR.always() 代替.

因此,如果您使用的是 jQuery 1.8 或更高版本,我们将需要更新成功和错误函数逻辑,例如:-

//发出请求后立即分配处理程序,//并记住这个请求的 jqXHR 对象var jqxhr = $.ajax("some_unknown_page.html").done(功能(响应){//这里的成功逻辑$('#post').html(response.responseText);}).fail(函数(jqXHR,异常){//我们的错误逻辑在这里var msg = '';如果(jqXHR.status === 0){msg = '未连接.
 验证网络.';} else if (jqXHR.status == 404) {msg = '找不到请求的页面.[404]';} else if (jqXHR.status == 500) {msg = '内部服务器错误 [500].';} else if (exception === 'parsererror') {msg = '请求的 JSON 解析失败.';} else if (异常 === '超时') {msg = '超时错误.';} else if (异常 === '中止') {msg = 'Ajax 请求中止.';} 别的 {msg = '未捕获的错误.
' + jqXHR.responseText;}$('#post').html(msg);}).总是(函数(){警报(完成");});

希望有帮助!

I have an ajax call passing data to a page which then returns a value.

I have retrieved the successful call from the page but i have coded it so that it raises an error in the asp. How do i retrieve that error from the jquery?

For example:

cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
    alert('successful : ' + html);
    $("#result").html("Successful");
},
error: function (error) {
    **alert('error; ' + eval(error));**
}

It's the error bit that I don't understand. In the function what parameter do I need to put, so that I can then use the error message that I raised in the server.

解决方案

The required parameters in an Ajax error function are jqXHR, exception and you can use it like below:

$.ajax({
    url: 'some_unknown_page.html',
    success: function (response) {
        $('#post').html(response.responseText);
    },
    error: function (jqXHR, exception) {
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.
 Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.
' + jqXHR.responseText;
        }
        $('#post').html(msg);
    },
});

DEMO FIDDLE


Parameters

jqXHR:

Its actually an error object which is looks like this

You can also view this in your own browser console, by using console.log inside the error function like:

error: function (jqXHR, exception) {
    console.log(jqXHR);
    // Your error handling logic here..
}

We are using the status property from this object to get the error code, like if we get status = 404 this means that requested page could not be found. It doesn't exists at all. Based on that status code we can redirect users to login page or whatever our business logic requires.

exception:

This is string variable which shows the exception type. So, if we are getting 404 error, exception text would be simply 'error'. Similarly, we might get 'timeout', 'abort' as other exception texts.


Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

So, in case you are using jQuery 1.8 or above we will need to update the success and error function logic like:-

// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax("some_unknown_page.html")
    .done(function (response) {
        // success logic here
        $('#post').html(response.responseText);
    })
    .fail(function (jqXHR, exception) {
        // Our error logic here
        var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.
 Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.
' + jqXHR.responseText;
        }
        $('#post').html(msg);
    })
    .always(function () {
        alert("complete");
    });

Hope it helps!

这篇关于jQuery ajax 错误函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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