循环中的jQuery GET请求

时间:2015-03-03 08:12:34

标签: php jquery ajax

我有一个像这样工作的脚本: 当你去网址

本地主机/ geocinema /的index.php?电影= 606

它将使用以下脚本在我的服务器上下载电影号码:

$.ajax({
        url: link,
        type: 'GET',
        beforeSend: function() {
            console.log("Downloading "+title);
        },
        complete: function() {
        },
        success: function(result) {
            console.log("Download Success for "+title);
        }
    });

其中“link”是处理下载的php文件。

现在我需要从1到600下载电影,所以我需要以某种方式遍历所有的URL。

我尝试从另一个文件发送get请求,如下所示:

$.ajax({
        url: 'http://localhost/geocinema/index.php?movie={1-600}',
        type: 'GET',
        beforeSend: function() {
            console.log("Downloading ");
        },
        complete: function() {
        },
        success: function(result) {
            console.log("Download Success ");
        }
    });

但是因为我在index.php中使用了get请求,所以它不起作用(它不会等到文件被下载)。

所以我可以下载所有文件的唯一方法是在浏览器中手动输入1到600的URL-s并等待下载,这不是很方便。 任何帮助将不胜感激,谢谢。

4 个答案:

答案 0 :(得分:0)

使用for循环:

for (var i = 1; i <= 600; i++) {
    $.ajax({
        url: 'http://localhost/geocinema/index.php?movie=' + i,
        type: 'GET',
        beforeSend: function() {
            console.log("Downloading ");
        },
        complete: function() {
        },
        success: function(result) {
            console.log("Download Success ");
        }
    });
}

答案 1 :(得分:0)

您可能想要创建一个ajax请求队列,因此每个请求都会等到上一个完成。如果你只是在for循环中启动所有ajax请求,那么所有这些请求将同时触发。如果我们上传600个视频,那将是一个非常困难的负载。因此,一种解决方案是使用带有ajax async标志的for循环:false。

for (var i = 1; i <= 600; i++) {
    $.ajax({
        url: 'http://localhost/geocinema/index.php?movie=' + i,
        type: 'GET',
        beforeSend: function() {
            console.log("Downloading ");
        },
        async: false,
        complete: function() {
        },
        success: function(result) {
            console.log("Download Success ");
        }
    });
}

另一个是定义一个在完成上传后递归调用自身的函数。

var currentMovie = 0;
var lastMovie = 600;

function getMovie() {
  $.ajax({
    url: 'http://localhost/geocinema/index.php?movie='+currentMovie,
    type: 'GET',
    beforeSend: function() {
      console.log("Downloading ");
    },
    complete: function() {
      currentMovie++;
      if (currentMoview<=lastMovie) {
        getMovie();
      }
    },
    success: function(result) {
      console.log("Download Success ");
    }
  });
}

getMovie();

答案 2 :(得分:0)

做这样的事情,它会在完成上一次调用后调用相同的函数

var i = 1;
function ajaxCall(){
    $.ajax({
        url: 'http://localhost/geocinema/index.php?movie=' + i,
        type: 'GET',
        beforeSend: function() {
            console.log("Downloading ");
        },
        complete: function() {
        },
        success: function(result) {
            console.log("Download Success ");
            if(i++ <= 600){ ajaxCall(); }
        }
    })
}
ajaxCall();

答案 3 :(得分:0)

我解决了这个问题:

    var url = window.location.href;
    var ind = url.indexOf("=")+1;
    var res = url.substr(ind)
    res = parseInt(res);
    res = res+1;
    var nxurl = "http://localhost/geocinema/index.php?movie="+res;

    $.ajax({
        url: link,
        type: 'GET',
        beforeSend: function() {
            console.log("Downloading "+title);
        },
        complete: function() {
        },
        success: function(result) {
            console.log("Download Success for "+title);
            window.location = nxurl;
        }
    });