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

前端 未结 11 1628
野的像风
野的像风 2020-11-22 14:17

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 onl

相关标签:
11条回答
  • 2020-11-22 14:22

    you can try it too:

    $(document).ajaxError(
        function (event, jqXHR, ajaxSettings, thrownError) {
            alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])');
        });
    
    0 讨论(0)
  • 2020-11-22 14:27

    Look at the responseText property of the request parameter.

    0 讨论(0)
  • 2020-11-22 14:27

    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));
    }
    
    0 讨论(0)
  • 2020-11-22 14:29

    The best simple approach :

    error: function (xhr) {
    var err = JSON.parse(xhr.responseText);
    alert(err.message);
    }
    
    0 讨论(0)
  • 2020-11-22 14:35

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

    error: function(xhr, status, error) {
      alert(error);
    }
    
    0 讨论(0)
  • 2020-11-22 14:39

    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'

    0 讨论(0)
提交回复
热议问题