这应该返回一个包含图片文件名列表的JSON对象.注释警报显示正确的数据,但alert(getPicsInFolder("testfolder"));显示"error".
function getPicsInFolder(folder) {
return_data = "error";
$.get("getpics.php?folder=" + folder, function (data) {
data = jQuery.parseJSON(data);
$.each(data, function (index, value) {
data[index] = "folders/" + folder + "/" + value;
});
//alert(data); // This alert shows the correct data, but that's hardly helpful
return_data = data;
});
return return_data;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我将数据从JQuery ajax调用传递回调用位置时出现问题.有问题的代码如下:
jQuery("#button").click(function()
{
for(var i = 0;i < data.length; i++)
{
result = updateUser(data[i]); //result is not populated..
alert(result); //prints 'undefined'
}
});
function updateUser(user_id)
{
jQuery.ajax({
url:"/users/update/"+user_id,
type:"GET",
async: false,
success: (function(data){
//if I alert "data" here it shows up correctly
//but if i try to return it like below
//it does not get passed correctly
return data;
})
});
Run Code Online (Sandbox Code Playgroud)
任何指针都非常感谢