我知道这是已知主题,解决方案之一是将调用更改为同步。 我仍然不清楚是否有其他方法可以异步执行并获取完整函数中的数据? 示例函数在 success 函数中创建了一个新的 asset 对象,我想在complete 函数中获取对它的引用。
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');
}
});
}
最佳答案
您可以简单地使用在 complete
事件中获得的 jQXhr
对象。
完整事件的实际签名是 complete(jqXHR, textStatus)
所以类似于
complete:function(jqXHR,status)
{
if(status == 'success' || status=='notmodified')
{
var asset = new asset(item.Type, item.Url, $.parseJSON(jqXHR.responseText))
}
}
关于jquery - 完整功能中如何获取jQuery ajax数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149199/