2010-09-03 118 views
1

什麼是最好的方式來獲得一個JSON響應到一個數組,我可以在下面的例子中使用? 這是我的函數調用觸發Ajax調用:json響應到數組

function getMaps(){ 

    mapID = "aus"; 
    mapImg = 'map_australia.jpg'; 

    $.ajax({ 
     type: "GET", 
     url: "getMap.asp", 
     data: "id=" + mapID, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(response) { 

      //not sure what to do here   
     } 

     }); 
     return //not sure what to return here 
     // it should resemble: return {id: 'aus', image: '/resources/images/maps/map_australia.jpg', data: '', maps: []}; 

}; 

出於測試目的getMap.asp發回follwoing:

{'j':[{'id':'aus','image':'/images/maps/map_detail.jpg','data':'','maps':[]}]} 
+0

你不想要返回一個數組,而是一個對象。 – BoltClock 2010-09-03 00:30:28

回答

1
return JSON.parse(response); 

如果你問如何處理異步響應,您需要重構調用getMaps的代碼,因爲它無法直接返回響應。相反,您應該接受回調作爲參數。