如何让jQuery的$。阿贾克斯错误响应的文字? [英] How to get the jQuery $.ajax error response text?

查看:140
本文介绍了如何让jQuery的$。阿贾克斯错误响应的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送错误回应我的jQuery。 但是,我不能得到响应文本(在下面这个例子是去了沙滩

I am sending an error response to my jQuery. However, I can not get the response text (in the example below this would be Gone to the beach)

唯一的jQuery说是'错误'。

The only thing jQuery says is 'error'.

请参阅本例中的细节:

PHP

<?
    header('HTTP/1.1 500 Internal Server Error');
    print "Gone to the beach"
?>

jQuery的

jQuery

$.ajax({
    type:     "post",
    data:     {id: 0},
    cache:    false,
    url:      "doIt.php",
    dataType: "text",
    error: function (request, error) {
        console.log(arguments);
        alert(" Can't do because: " + error);
    },
    success: function () {
        alert(" Done ! ");
    }
});

现在我的结果北京时间:

Now my result ist:

日志:

 [XMLHttpRequest readyState=4 status=500, "error", undefined]

警告:

不能这样做,因为:误差

Can't do because: error

任何想法?

推荐答案

尝试:

error: function(xhr, status, error) {
  var err = eval("(" + xhr.responseText + ")");
  alert(err.Message);
}

也看在<一个href="http://encosia.com/2009/03/04/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/">this encosia文章适当的错误处理。

这篇关于如何让jQuery的$。阿贾克斯错误响应的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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