4
可能重复:
return from jquery ajax call
jQuery: Return data after ajax call success
$.ajax({
url:"list.php",
dataType: "json",
success: function(resp){
for(var i=0;i<resp.length;i++){
$('#goods_list').append(
"<h3>" + resp[i]["name"] + "</h3>"
+
"<div>Price: NT$" + resp[i]["price"] + "<br />"
+
resp[i]["description"]
+
"<hr /> <a href=# alt='"+ resp[i]["sn"]+ "' class='add_to_cart'>Add</a> | More"
+"</div>"
);
}
var resp2 = resp;
}
});
$('body').append(resp2[0]["price"]);
而且萤火说:
ReferenceError: resp2 is not defined
$('body').append(resp2[0]["price"]);
如何使用$.ajax
成功的数据在什么地方其他? ($ .ajax函数之外)
该概念与“全局变量”类似。
你不能因为请求是异步的(除非你让它同步,这会阻止你的脚本)。您必须将该行移至成功处理程序。 –
我不完全确定你的模式,但你也可以在函数中返回ajax调用,然后在任何地方使用'func()。done(...)'。 – pimvdb