How to get “Data” field from xhr.responseText?

后端 未结 7 1090
别跟我提以往
别跟我提以往 2020-12-09 14:40

I have XMLHttpRequest() function given below

var searchFriendRequests = function (userid) {
    var xhr = new XMLHttpRequest();
    xhr.open(\'G         


        
7条回答
  •  没有蜡笔的小新
    2020-12-09 15:12

    When you make your ajax request you can provide dataType option:

    dataType: 'json'
    

    For such request when you receive response:

    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.

    you can access your data like:

    var data =  xhr.responseJSON
    

    Full example:

      $ajax.({
        dataType: 'json',
        success: function( xhr ) {
          var yourData =  xhr.responseJSON;
          console.log( yourData );
        },
      });
    

提交回复
热议问题