获取jQuery get()。done()函数的返回值

我有代码:

function images(){ $.get("page").done(function(data) { manipulation to get required data }); } 

我试图从get函数中获取一个变量,使用全局变量,返回值和函数外部的函数。 有没有人知道我如何从get函数内部获取变量,并在调用images()将其作为值返回?

你不能这样做,因为$.get()异步执行的。 解决方案是在图像中使用回调来处理$ .get()返回的值

 function images(callback){ $.get("page").done(function(data) { manipulation to get required data var d = ? //manipulated data that was supposed to be returned callback(d); }); } 

然后

 //calls images images(function(data){ //this method will get called when the $.get() is completed, and data will be the value passed to callback(?) in the done function }) 

更多: 阅读本文