2016-10-25 133 views
-3

我重視API在我的javascript其中有這樣的結果,當我通過郵遞員貼:解析JSON從API爲Javascript

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

和這裏的API腳本:

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

} 
}); 

我怎麼能添加json結果的內容填充這個空的var?

var address = ????? 
var client_name = ????? 
+2

的jQuery已經解碼JSON響應轉換成常規的JavaScript對象。只要閱讀其他任何對象即可。 –

+0

是的,所以將它添加到選項中:type:「POST」,dataType:「JSON」,url:... –

+1

var address = data.result.address; var client_name = data.result.client_name' – Danmoreng

回答

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

謝謝,這對我很有用。此外,開發人員表示,它可能會使用jsonp自跨源請求。我應該做點別的嗎? –

+1

JSONP是JSON的擴展,用於擺脫跨域限制。您必須將腳本加載爲JSON +回調函數。但不幸的是,我非常想成爲JSONP的專家。我建議你:http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about –