json响应到数组
问题描述:
什么是最好的方式来获得一个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':[]}]}
答
return JSON.parse(response);
如果你问如何处理异步响应,您需要重构调用getMaps
的代码,因为它无法直接返回响应。相反,您应该接受回调作为参数。
你不想要返回一个数组,而是一个对象。 – BoltClock 2010-09-03 00:30:28