Search code examples
javascriptarraysjsonapisend

Parse Json from API to Javascript


I have attached API on my javascript which has this result when I posted via postman:

{"result": {
    "clientId": 1,
    "session": "yJZdWRhIjoiSEExMDA4MzAyMCJ9.wrBz8JpNqMZlqv8Pz2Tx1x-XYecfdsH2B5uTbNPfiQE",
    "client name": Andrew,
    "address": [
      ["1","Bandung","West Java","Indonesia"],
      ["2","Depok","West Java","Indonesia"]
  }
}

and here's the API script:

var content='';
$.ajax({
type: "POST",
url:
rest_url+
'update/yJZdWRhIjoiSEExMDA4MzAyMCJ9.wrBz8JpNqMZlqv8Pz2Tx1x-XYecfdsH2B5uTbNPfiQE/'+_ClientId,
success: function (data) {
_Client  = data;

}
});

How could I add the content of json result to fill this empty var?

var address = ?????
var client_name = ?????

Solution

  • $.ajax({
             type: 'POST',
             ....
             ....
             dataType: 'json', // don't forget !!!
             success: function(data) {
                          var address = data.result.address;