2016-05-17 72 views
0

我有下面的代码时,它同步,我传递参数给它如何将参数传递给jquery异步json?

function getValues(itemid){ 
     var parsedpage = $.ajax({ 
      cache  : false, 
      async  : false, 
      url   : "http://cors.io/?u=http://cata.openwow.com/item="+itemid, 
      dataType : "text", 
     }); 
    return parsedpage.responseText; 
} 
getValues(70266); 
getValues(70378); 
getValues(70268); 

有更多getValues调用导致加载时间长和用户体验差,只有与Tampermonkey工作,想我应该重构它的工作同步,如果我将async:false更改为async:true该代码不适用于循环18个数字的循环。它必须以responseText作为html代码这样做,因为网站并不真正支持json,或者我无法找到它,因为它根本没有记录。

如何重写我的函数以工作异步?我读过How do I return the response from an asynchronous call?但不包括通过我自己的函数参数

+0

看起来像你需要一个'promise()' – Derek

+0

jQuery''.when''就足够了吗? –

+0

是的,它应该是 – Derek

回答

-1

好吧,我设法与添加功能为succes: Ajax来解决这个问题,并得到了我所需要的

function getValues(itemid){ 
    $.ajax({ 
     cache  : false, 
     async  : true, 
     url   : "http://cors.io/?u=http://cata.openwow.com/item="+itemid, 
     dataType : "text", 
     success: function (data) { 
      var parsedpage = data; 
     }, 
    }); 
} 
+0

你不能从异步调用 – epascarello

+0

返回哦,是的,有回报的行根本不需要 –