0

我正在尝试向 facebook 运行 x 个 ajax 请求并将数据对象存储在一个数组中,以便我可以在我的页面上使用这些数据。例如,我想使用如下方式调用数据:dataStore[0].data.likes

我遇到了将 ajax 请求检索到的数据对象推送到数组中的问题。感谢您的任何帮助,您可以提供!

var busNames = ['page1', 'page2', 'page3', 'page4', 'page5', 'page6'];
var dataStore = [];

  for(i=0; i<busNames.length; i+=1){
      $.ajax({
      url: 'http://graph.facebook.com/' + busNames[i] +'/?limit=5&callback=?',
      dataType: 'json',
      success: function(data, status) {
          dataStore.push(data);
      },
      error: function(data, e1, e2) {console.log(data + e1 + e2);
      }
    });// end ajax

  }// end loop

console.log( busNames[0] + " Likes: " + dataStore[0].data.likes);
console.log( busNames[1] + " Likes: " + dataStore[1].data.likes);
console.log( busNames[2] + " Likes: " + dataStore[2].data.likes);
console.log( busNames[3] + " Likes: " + dataStore[3].data.likes);
console.log( busNames[4] + " Likes: " + dataStore[4].data.likes);
console.log( busNames[5] + " Likes: " + dataStore[5].data.likes);

JSFiddle:http: //jsfiddle.net/BJPvs/

4

2 回答 2

1

实际上,您的代码工作正常,但是在将数据推送到 dataStore 数组之前,最终会执行 6 次打印到控制台。那是因为成功函数是一个回调。因此,如果您在回调内部打印到控制台,您会看到所有数据都被推送到数组中就好了:-)

var busNames = ['cocacola', 'apple', 'mtndew', 'drpepper', 'page5', 'page6'];
var dataStore = [];

  for(i=0; i<busNames.length; i+=1){
      $.ajax({
      url: 'http://graph.facebook.com/' + busNames[i] +'/?limit=5&callback=?',
      dataType: 'json',
      success: function(data, status) {
          dataStore.push(data);
          console.log(dataStore);
      },
      error: function(data, e1, e2) {console.log(data + e1 + e2);
      }
    });// end ajax
  }// end loop
// });

/*console.log( busNames[0] + " Likes: " + dataStore[0].data.likes);
console.log( busNames[1] + " Likes: " + dataStore[1].data.likes);
console.log( busNames[2] + " Likes: " + dataStore[2].data.likes);
console.log( busNames[3] + " Likes: " + dataStore[3].data.likes);
console.log( busNames[4] + " Likes: " + dataStore[4].data.likes);
console.log( busNames[5] + " Likes: " + dataStore[5].data.likes);*/

JSFiddle:http: //jsfiddle.net/BJPvs/1/

于 2013-10-19T20:42:46.217 回答
1
var busNames = ['cocacola', 'apple', 'mtndew', 'drpepper', 'page5', 'page6'];
var dataStore = [];

for (i = 0; i < busNames.length; i += 1) {
    (function (index) {
        $.ajax({
            url: 'http://graph.facebook.com/' + busNames[i] + '/?limit=5&callback=?',
            dataType: 'json',
            success: function (data, status) {
                dataStore.push(data);
                //console.log(busNames[index] + " Likes: " + data.likes);
            },
            error: function (data, e1, e2) {
                console.log(data + e1 + e2);
                dataStore.push(data);
            }
        }); // end ajax
    })(i)
} // end loop


// grab them afterward.
function wait() {
    if (dataStore.length == busNames.length) {
        for (var i = 0; i < dataStore.length; i++) {
            console.log(busNames[i] + " Likes: " + dataStore[i].likes);
        }
    } else {
        setTimeout(wait, 100);
    }
}

wait();

现场演示

更好的方法我确定,但我所做的只是将它包装在一个闭包中并将索引传递给函数。添加了一个功能,该功能会等到值已加载后再将它们注销到控制台。

于 2013-10-19T20:46:59.730 回答