我正在尝试执行 ajax 请求
$.ajax({
type: "post",
url: "download.php",
error: function(data, status, err){
alert(JSON.stringify(data));
},
data: "fileid="+fileid
});
此请求提醒“{"readyState":0,"responseText":"","status":0,"statusText":"error"}"
我在谷歌上搜索过,我得到的只是跨站点ajax调用(这显然不是)
我尝试输入完整的网址,效果相同。
我唯一能想到的是标题,我不知道它会出现什么问题。这是来自 firebug 的请求 header
Host www.mydomain.com
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://www.mydomain.com/
Content-Length 8
Cookie PHPSESSID=27b7d3890b82345a4fc9604808acd928
我在不同的页面上添加了另一个请求,它工作得很好,但是这个请求一直失败,另一个请求的 header 是:
Host www.mydomain.com
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept text/plain, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
Referer http://www.mydomain.com/differentpage.php
Content-Length 33
Cookie PHPSESSID=27b7d3890b82345a4fc9604808acd928
最佳答案
我遇到了同样的问题:如何在用户每次单击链接时进行注册。
问题实际上是,如果你不停止弹出窗口,ajax 请求就不会完成,并且你会得到readyState: 0!
我已经完成了上述内容的另一个版本,可能更具可读性(即使更冗长)
/* --------------------------------------------------------------------------
* Before that add 'downloads' class to every anchor tag (link) in your page
* This script does the rest
*
* remember to change 'your_php_file' with the one you use
* -------------------------------------------------------------------------- */
$(document).ready( function()
{
// Check if there is any link with class 'downloads'
if ( typeof $('.downloads') != 'undefined' )
{
var links = $('.downloads');
// Run this for every download link
for ( var i = 0; i < links.length; i++ )
{
// Set click behaviour
links[i].onclick = function(e)
{
// Get download name
var attr = this.attributes,
href = attr.href.textContent,
elem = href.split('/'),
elem = elem[elem.length - 1];
// Send the download file name and only after completing the request let the user download the file
$.ajax(
{
type : "POST",
dataType : "text",
// 'your_php_file' must be an ABSOLUT or RELATIVE path!
url: your_php_file,
// 'elem' is a variable containing the download name
// you can call it in your php file through $_POST['download_name']
data: { download_name: elem },
// here we go magic:
// after the request is done run the popup for the download
complete: function()
{
window.location.href = href;
}
});
// Stop default behaviour until ajax request has been done
e.preventDefault();
};
}
}
});
关于jquery ajax错误{"readyState":0 ,"responseText" :"" ,"status":0 ,"statusText" :"error"},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7018247/