在单击文件下载时,成功重定向到使用AJAX的另一个页面

在我的WordPress项目中,我的下载按钮包含一个.zip文件,应该下载onClick。 所以HTML的产生是:

 DOWNLOAD 

我正在使用AJAX刷新下载次数。

  var ajaxurl = ''; jQuery(document).on('click', '.download-link', function () { var id = this.id; jQuery.ajax({ type: 'POST', url: ajaxurl, data: {"action": "count_download", "id": id }, success: function (data) { window.location = site.url + "/download-success?fid="+ id; } }); });  

一切正常,直到我添加链接文件。 通常这样的链接将开始下载.zip文件,但即使在AJAX调用所花费的时间之后,页面也会重定向到下载成功页面而不会触发下载。

它大部分时间都会发生,只有一次或两次文件开始下载。

PS:我测试了这个,但不是我的情况。

同意@marc。 只是为了添加更多信息,请执行此操作。

 Download 

以上可以是url链接下载,下面将是download.php中的php代码

 //code to update download count (UPDATE tbl_dwn SET total = total + 1) //below is code to download (hope you know it) $zipName = $_GET['file']; //here you've to specify the absolute path to the download.zip file header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=".$zipName.""); header("Content-length: " . filesize($zipName)); header("Pragma: no-cache"); header("Expires: 0"); readfile($zipName); 

试试这个,如果你有任何问题,请告诉我。

试试这个,

将带你到另一页

 success: function (data) { window.location.href = site.url + "/download-success?fid="+ id; } 

将带您进入新窗口中的另一个页面

 success: function (data) { window.open('site.url + "/download-success?fid="+ id'); }