如何获得jQuery $。ajax错误响应文本?

时间:2022-10-09 07:27:54

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'.

jQuery唯一说的是“错误”。

See this example for details:

详细信息见此示例:

php

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:

现在我的结果是:

log:

日志:

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

alert:

警告:

Can't do because: error

不能因为:错误

Any ideas?

什么好主意吗?

10 个解决方案

#1


241  

Try:

试一试:

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

#2


45  

Look at the responseText property of the request parameter.

查看请求参数的responseText属性。

#3


38  

For me, this simply works:

对我来说,这很简单:

error: function(xhr, status, error) {
  alert(xhr.responseText);
}

#4


7  

As ultimately suggested by this other answer and it's comments on this page:

最后由另一个答案和它在这一页上的评论提出:

error: function(xhr, status, error) {
  var err = JSON.parse(xhr.responseText);
  alert(err.Message);
}

#5


5  

This is what worked for me

这就是我的工作。

    function showErrorMessage(xhr, status, error) {
        if (xhr.responseText != "") {

            var jsonResponseText = $.parseJSON(xhr.responseText);
            var jsonResponseStatus = '';
            var message = '';
            $.each(jsonResponseText, function(name, val) {
                if (name == "ResponseStatus") {
                    jsonResponseStatus = $.parseJSON(JSON.stringify(val));
                     $.each(jsonResponseStatus, function(name2, val2) {
                         if (name2 == "Message") {
                             message = val2;
                         }
                     });
                }
            });

            alert(message);
        }
    }

#6


3  

you can try it too:

你也可以试试:

$(document).ajaxError(
    function (event, jqXHR, ajaxSettings, thrownError) {
        alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])');
    });

#7


2  

If you want to get Syntax Error with line number, use this

如果希望使用行号获得语法错误,请使用它。

error: function(xhr, status, error) {
  alert(error);
}

#8


2  

This will allow you to see the whole response not just the "responseText" value

这将允许您看到整个响应,而不仅仅是“responseText”值。

error: function(xhr, status, error) {
    var acc = []
    $.each(xhr, function(index, value) {
        acc.push(index + ': ' + value);
    });
    alert(JSON.stringify(acc));
}

#9


0  

If you're not having a network error, and wanting to surface an error from the backend, for exmple insufficient privileges, server your response with a 200 and an error message. Then in your success handler check data.status == 'error'

如果您没有网络错误,并且想要从后端出现错误,那么您需要使用200和一个错误消息来处理不足的权限,服务器您的响应。然后在您的成功处理程序检查数据。状态= =‘错误’

#10


0  

The best simple approach :

最简单的方法:

error: function (xhr) {
var err = JSON.parse(xhr.responseText);
alert(err.message);
}

#1


241  

Try:

试一试:

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

#2


45  

Look at the responseText property of the request parameter.

查看请求参数的responseText属性。

#3


38  

For me, this simply works:

对我来说,这很简单:

error: function(xhr, status, error) {
  alert(xhr.responseText);
}

#4


7  

As ultimately suggested by this other answer and it's comments on this page:

最后由另一个答案和它在这一页上的评论提出:

error: function(xhr, status, error) {
  var err = JSON.parse(xhr.responseText);
  alert(err.Message);
}

#5


5  

This is what worked for me

这就是我的工作。

    function showErrorMessage(xhr, status, error) {
        if (xhr.responseText != "") {

            var jsonResponseText = $.parseJSON(xhr.responseText);
            var jsonResponseStatus = '';
            var message = '';
            $.each(jsonResponseText, function(name, val) {
                if (name == "ResponseStatus") {
                    jsonResponseStatus = $.parseJSON(JSON.stringify(val));
                     $.each(jsonResponseStatus, function(name2, val2) {
                         if (name2 == "Message") {
                             message = val2;
                         }
                     });
                }
            });

            alert(message);
        }
    }

#6


3  

you can try it too:

你也可以试试:

$(document).ajaxError(
    function (event, jqXHR, ajaxSettings, thrownError) {
        alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])');
    });

#7


2  

If you want to get Syntax Error with line number, use this

如果希望使用行号获得语法错误,请使用它。

error: function(xhr, status, error) {
  alert(error);
}

#8


2  

This will allow you to see the whole response not just the "responseText" value

这将允许您看到整个响应,而不仅仅是“responseText”值。

error: function(xhr, status, error) {
    var acc = []
    $.each(xhr, function(index, value) {
        acc.push(index + ': ' + value);
    });
    alert(JSON.stringify(acc));
}

#9


0  

If you're not having a network error, and wanting to surface an error from the backend, for exmple insufficient privileges, server your response with a 200 and an error message. Then in your success handler check data.status == 'error'

如果您没有网络错误,并且想要从后端出现错误,那么您需要使用200和一个错误消息来处理不足的权限,服务器您的响应。然后在您的成功处理程序检查数据。状态= =‘错误’

#10


0  

The best simple approach :

最简单的方法:

error: function (xhr) {
var err = JSON.parse(xhr.responseText);
alert(err.message);
}