如何获取 jQuery $.ajax 错误响应文本? [英] How to get the jQuery $.ajax error response text?

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

问题描述

我正在向我的 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'.

有关详细信息,请参阅此示例:

See this example for details:

php

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

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 ! ");
    }
});

现在我的结果是:

日志:

 [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);
}

这篇关于如何获取 jQuery $.ajax 错误响应文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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