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

2018-12-31 22:04发布

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?

11条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 22:06

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);
    }
}
查看更多
人气声优
3楼-- · 2018-12-31 22:07

you can try it too:

$(document).ajaxError(
    function (event, jqXHR, ajaxSettings, thrownError) {
        alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])');
    });
查看更多
牵手、夕阳
4楼-- · 2018-12-31 22:10

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楼-- · 2018-12-31 22:11

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楼-- · 2018-12-31 22:12

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));
}
查看更多
明月照影归
7楼-- · 2018-12-31 22:14

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

error: function(xhr, status, error) {
  alert(error);
}
查看更多
登录 后发表回答