我知道这是众所周知的主题,解决方案之一是将调用更改为同步。
我仍然不清楚是否有其他方法可以异步执行并获取完整函数中的数据?
示例函数在 success 函数中创建一个新的资产对象,我想在完整函数中获取对它的引用。
function getPresentation(item) {
$.ajax({
type: "GET",
url: item.Url,
success: function (data) {
assets.push(new asset(item.Type, item.Url, data));
},
complete: function () {
/// How to get here the reference for the newly created asset object?
/// how to alert(asset)?
},
error: function (req, status, error) {
alert('error');
}
});
}
您可以简单地使用jQXhr
你得到的对象complete
事件。
完整事件的实际签名是complete(jqXHR, textStatus)
所以类似的东西
complete:function(jqXHR,status)
{
if(status == 'success' || status=='notmodified')
{
var asset = new asset(item.Type, item.Url, $.parseJSON(jqXHR.responseText))
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)