2012-04-13 66 views
2

我知道这是已知的主题,其中一个解决方案是将呼叫更改为同步。 还是不清楚,如果有任何其他方式来做到异步,并获得完整功能的数据? 示例函数在成功函数中创建一个新的资产对象,我想获得完整函数的引用。如何在竞争函数中获取jQuery ajax数据?

 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'); 
      } 
     }); 

    } 

回答

4

您可以简单地使用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)) 
} 
} 
+0

u的得到任何特定的错误的线,所以somethng? – 2012-04-13 23:03:52

+0

即时假设你得到JSON作为服务器的数据 – 2012-04-13 23:05:11

+0

当然是json – 2012-04-13 23:06:38