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

前端 未结 11 1629
野的像风
野的像风 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:42

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

    For me, this simply works:

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

    I used this, and it worked perfectly.

    error: function(xhr, status, error){
         alertify.error(JSON.parse(xhr.responseText).error);
    }
    
    0 讨论(0)
  • 2020-11-22 14:48

    Try:

    error: function(xhr, status, error) {
      var err = eval("(" + xhr.responseText + ")");
      alert(err.Message);
    }
    
    0 讨论(0)
  • 2020-11-22 14:49

    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题