如何通过AJAX发送数千个对象?

时间:2013-12-31 17:32:48

标签: javascript ajax performance jquery

我正在尝试使用他们的AJAX API将100K +记录加载到云服务中。它看起来像这样:

var multiple = [{ "Author": "Sample Text" }, { "Author": "Second Sample Text" },...];

$.ajax({
    type: "POST",
    url: 'http://api.cloudservice.com/v1/your-api-key-here/Books',
    headers: { "Authorization": "Bearer your-access-token-here" },
    contentType: "application/json",
    data: JSON.stringify(multiple),
    success: function (data) {...},
    error: function (error) {...}
})

我当然不想发送100K记录的有效载荷,所以我可以一次将其减少到100个。即便如此,在循环中执行此操作仍然会很大。我如何创建可靠的AJAX循环来执行此操作或以其他方式?我也希望以某种方式可靠地记录所有错误。任何经验或建议将不胜感激!

4 个答案:

答案 0 :(得分:0)

使用ajax调用递归列表,将其分解为100(或您设置的任何内容)。

var multiple = [{ "Author": "Sample Text" }, { "Author": "Second Sample Text" }];
(function recursePost(set, iter, take){
 var group = iter + take < set.length ? iter + take: set.length - iter;
 var dataObj = [];
 for( var i = iter; i < group; i++ ){
  dataObj.push(set[i]);
 }
 iter += group;
 $.ajax({
  type: "POST",
  url: 'http://api.cloudservice.com/v1/your-api-key-here/Books',
  headers: { "Authorization": "Bearer your-access-token-here" },
  contentType: "application/json",
  data: JSON.stringify(dataObj),
  success: function (data) {
    recursePost(set, iter, take);
  },
  error: function (error) {
    alert("Post Failed at " + (iter-group) + "-" + group);
  }
 });
})(multiple,0,100)

答案 1 :(得分:0)

创建一个方法,使用数组上的slice方法调用它来发送一堆你需要的大小,手动调用一次,如果有更多记录,则让你的ajax再次调用它: / p>

// obviously don't use globals in *your* code...
var current_count = 0;
var batch_size = 100;
var multiple = [{ "Author": "Sample Text" }, { "Author": "Second Sample Text" }... ];
var end_index = multiple.length() - 1;

function send_batch() {
   var batch_end = (current_count + batch_size) < end_index ? current_count + batch_size : end_index;
   var batch = multiple.slice(current_count, batch_end);
   current_count = batch_end;
   $.ajax({
     type: "POST",
     url: 'http://api.cloudservice.com/v1/your-api-key-here/Books',
     headers: { "Authorization": "Bearer your-access-token-here" },
     contentType: "application/json",
     data: JSON.stringify(batch),
     success: function (data) {
        if (batch_end <= end_index) {
           send_batch();
        }
     },
     error: function (error) {...}
    });
};

你可以提取Ajax调用并对事物进行参数化以使其更通用,但希望你能得到这个想法。

答案 2 :(得分:0)

没有机会测试这个,但你明白了。批处理,排序,但使用异步而不是同步,因此您不必等待回调。您也可以在递归中执行此操作,但在JS中递归总是比循环标准需要更多的能量。

var x=null, tempArr=[], batch_size = 100,
    multiple = [{ "Author": "Sample Text" }, { "Author": "Second Sample Text" }... ];

for(x in multiple){
  tempArr = (multiple.length > 100) ? multiple.slice(0,99):  multiple.slice(0,multiple.length-1);
  multiple = multiple.slice(100, multiple.length-1);
  if (multiple.hasOwnProperty(x){
    $.ajax({
     type: "POST",
     url: 'http://api.cloudservice.com/v1/your-api-key-here/Books',
     headers: { "Authorization": "Bearer your-access-token-here" },
     contentType: "application/json",
     data: JSON.stringify(tempArr)
    })
    .done(function (data) {
      //process data returned
    });
  }
}

答案 3 :(得分:0)

如果要同时进行ajax调用,可以执行以下操作:

var BATCH_SIZE = 100,
    calls = [];
for (var i = 0; i < multiple.length; i += BATCH_SIZE) {
    var batch = multiple.slice(i, i + BATCH_SIZE);
    calls.push($.ajax({
        type: 'post',
        url: 'http://api.cloudservice.com/v1/your-api-key-here/Books',
        headers: { "Authorization": "Bearer your-access-token-here" },
        contentType: "application/json",
        data: JSON.stringify(batch)
    }));
}
$.when.apply($, calls).done(function () {
    alert('last call finished');
});

jsfiddle