我知道这是一个已知主题,解决方案之一是将调用更改为同步。我仍然不清楚是否还有其他方法可以异步执行并在完整函数中获取数据?示例函数在成功函数中创建了一个新的资产对象,我想在完整的函数中获取对它的引用。
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');
}
});
}
Run Code Online (Sandbox Code Playgroud)
You can simply use the jQXhr object you get in the complete event.
The actual signature of the complete event is complete(jqXHR, textStatus)
so somethng along the lines of
complete:function(jqXHR,status)
{
if(status == 'success' || status=='notmodified')
{
var asset = new asset(item.Type, item.Url, $.parseJSON(jqXHR.responseText))
}
}
Run Code Online (Sandbox Code Playgroud)