简体   繁体   中英

Getting list back from ajax jsonp callback

I am making an ajax call via jquery with a jsonp callback function. The callback gets called and generates a list for me that I need to return to the original alling function and assign to a variable. However, it is not getting passed back. I know I am doing something wrong or misunderstanding how this flow works. Could somebody please point me in the correct direction? Here is the (abbreviated) code:

function() {
  ..build url...
  var multiTargets = getMultiMetrics(url);
  ...do stuff with list...
}



getMultiMetrics = function(url) {
  $.ajax({
    url: url,
    jsonp : true,
    jsonpCallback: 'metricCallback',
    cache: true,
    dataType : 'jsonp',
    async: false
});
};

metricCallback = function(data) {
  var items = [];
  for (var i = data.length - 1; i >= 0; i--) {
    items.push(data[i].target);
  };
  return items;
};

尽量不要在jsonpCallback: metricCallback,使用引号jsonpCallback: metricCallback,此参数必须是函数而不是字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM