2015-03-03 93 views
1

請求我有一個腳本,是這樣的:? 當你去到URLjQuery的獲得環

本地主機/ geocinema/index.php的電影= 606

將下載的電影數量606我的服務器與此腳本:

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

其中「鏈接」是處理下載的PHP文件。

現在我需要下載電影從1到600,所以我需要以某種方式循環所有的URL-s。

我試圖從另一個文件發送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並等待下載,這不是很方便。 任何幫助將不勝感激,謝謝。

回答

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 "); 
     } 
    }); 
} 
+0

我寫的在第二部分中,我嘗試過,但由於我在ind中使用了另一個GET請求ex.php它不起作用(它不會等到index.php中的請求完成,所以文件不會被下載) – 2015-03-03 08:20:35

+0

我認爲這可能是一個問題。您應該循環使用'link'的AJAX請求,並使用適當的參數修改該鏈接。 – Barmar 2015-03-03 08:22:09

+0

link是什麼樣的? – Barmar 2015-03-03 08:22:45

1

您可能希望創建Ajax請求的隊列,所以每個等待,直到前面的完成。如果您只是在for循環中啓動所有ajax請求,那麼它們都將同時觸發。如果我們上傳600個視頻,這將是一個非常艱難的負載。所以一個解決方案是使用for循環與ajax異步標誌: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();

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(); 
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; 
     } 
    });