【问题标题】:Call Ajax Again in for loop在 for 循环中再次调用 Ajax
【发布时间】:2014-05-13 11:30:50
【问题描述】:

我想在for 循环中多次请求ajax。但它只返回 1 个值。 result.ID。谁能帮帮我?

  $('#show').html('');
  var min = parseInt($('#number_coupon_from').val());
  var max = parseInt($('#number_coupon_to').val());
  var total_time = parseInt($('#time_coupon').val())*1000;
  var randcoupon = Math.floor(Math.random()*(max-min+1)+min);
  var timetb = total_time/randcoupon;
  var random = 0;
  var i,j;
  for(i=0;i<randcoupon;i++)
  {
      for(j=random;j>=0;j--)
      {
        if(j==0)
        {
          $.ajax({
          url: 'backend/publish_auto/update',
          type: 'post',
          data: {},
          dataType: 'json',
          success: function (result) 
          {
            if(result.st)
            {
              $('#show').append('Update Coupon ID: '+result.ID+'<br />');
            }
          }
          });
        }
      }
  }
});

谢谢!!

【问题讨论】:

  • 这不是使用 Ajax 的好方法
  • 你想用这个来达到什么目的?
  • 先检查randcoupon &gt; 0,内循环的目的是什么?
  • @Manibharathi:randcoupon 总是 > 0。 for 循环正常,但 ajax 请求仅 1 次。结果返回时间=for循环但返回一个值
  • @user3631551 工作正常,检查每个 ajax 响应中设置的 result.st 值。

标签: php ajax loops for-loop


【解决方案1】:

使用async:False一一获取ajax响应

$.ajax({
          url: 'backend/publish_auto/update',
          async: false,
          type: 'post',
          data: {},
          dataType: 'json',
          success: function (result) 
          {
            if(result.st)
            {
              $('#show').append('Update Coupon ID: '+result.ID+'<br />');
            }
          }
          });

【讨论】:

  • 谢谢@user3176644:但是ajax响应一次就死了:(。想要它响应很多次:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
相关资源
最近更新 更多