jQuery Ajax - how to get response data in error

后端 未结 4 1075
抹茶落季
抹茶落季 2020-12-13 08:23

I have a simple web application. I\'ve created the server REST API so it will return a response with HTTP code and a JSON (or XML) object with more details: application code

相关标签:
4条回答
  • 2020-12-13 08:52

    Here's an example of how you get JSON data on error:

    $.ajax({
        url: '/path/to/script.php',
        data: {'my':'data'},
        type: 'POST'
    }).fail(function($xhr) {
        var data = $xhr.responseJSON;
        console.log(data);
    });
    

    From the docs:

    If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

    Otherwise, if responseJSON is not available, you can try $.parseJSON($xhr.responseText).

    0 讨论(0)
  • 2020-12-13 09:06

    directly from the docs

    The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader()

    so use the jqXRH argument and get the responseText property off it.

    In the link above, look for the section entitled

    The jqXHR Object

    0 讨论(0)
  • 2020-12-13 09:06

    After spending so much time on this problem, I found the problem.

    The page is under the URL: www.mydomain.com/register
    The REST api is under the URL: server.mydomain.com/rest

    Seems like this kind of POST is not so simple.
    I'm going to search more information to understand this issue better (if you have more information please share it with me).

    When putting the REST API under www.mydomain.com/rest - everything is working fine.

    0 讨论(0)
  • 2020-12-13 09:07

    I also faced same problem when i was using multipart/form-data. At first I thought multipart/form-data created this mess, but later i found the proper solution.

    1) JS code before:

    var jersey_url = "http://localhost:8098/final/rest/addItem/upload";
    var ans = $.ajax({
        type: 'POST',
        enctype: 'multipart/form-data',
        url: jersey_url,
        data: formData,
        dataType: "json",
        processData: false,
        contentType: false 
        success : funtion(data){
          var temp = JSON.parse(data);
          console.log("SUCCESS : ", temp.message);  
        }
        error : funtion($xhr,textStatus,errorThrown){
           console.log("ERROR : ", errorThrown);
           console.log("ERROR : ", $xhr);
           console.log("ERROR : ", textStatus);
        }
        });
    

    Here when error occurred, it showed me this in console :-
    Error :
    Error : { abort : f(e), always : f(), .... , responseJSON :"{"message":"failed"}" }
    Error : error

    Thus i came to know that we have to use $xhr.responseJSON to get the string message which we sent from rest api.

    2) modified/working error funtion:

    error : funtion($xhr,textStatus,errorThrown){
            var string= $xhr.responseJSON;
            var json_object= JSON.parse(string);
            console.log("ERROR : ",  json_object.message);
        }
    

    Thus will output "Error : failed" on console.

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