get ajax error翻译,How to get the jQuery $.ajax error response text?

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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)

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

}

});

Now my result ist:

log:

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

alert:

Can\'t do because: error

Any ideas?

回答1:

Try:

error: function(xhr, status, error) {

var err = eval(\"(\" + xhr.responseText + \")\");

alert(err.Message);

}

回答2:

Look at the responseText property of the request parameter.

回答3:

For me, this simply works:

error: function(xhr, status, error) {

alert(xhr.responseText);

}

回答4:

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:

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:

you can try it too:

$(document).ajaxError(

function (event, jqXHR, ajaxSettings, thrownError) {

alert(\'[event:\' + event + \'], [jqXHR:\' + jqXHR + \'], [ajaxSettings:\' + ajaxSettings + \'], [thrownError:\' + thrownError + \'])\');

});

回答7:

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

error: function(xhr, status, error) {

var acc = []

$.each(xhr, function(index, value) {

acc.push(index + \': \' + value);

});

alert(JSON.stringify(acc));

}

回答8:

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

error: function(xhr, status, error) {

alert(error);

}

回答9:

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

}

}

回答10:

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

回答11:

The best simple approach :

error: function (xhr) {

var err = JSON.parse(xhr.responseText);

alert(err.message);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值