ajax xhr LengthComputeable返回false与php文件
我正在使用XMLHttpRequest执行一个ajax请求,以显示请求的进度。它在html文件中工作得很好,但在php文件中evt.lengthComputeable返回false 我的php文件是用utf-8编码的,不包含任何特殊内容ajax xhr LengthComputeable返回false与php文件,php,xmlhttprequest,Php,Xmlhttprequest,我正在使用XMLHttpRequest执行一个ajax请求,以显示请求的进度。它在html文件中工作得很好,但在php文件中evt.lengthComputeable返回false 我的php文件是用utf-8编码的,不包含任何特殊内容 xhr: function() { console.log('xhr'); var xhr = new XMLHttpRequest();
xhr: function()
{
console.log('xhr');
var xhr = new XMLHttpRequest();
xhr.addEventListener('loadend', uploadComplete, false);
function uploadComplete(event) {
console.log('uploadComplete');
//do stuff
}
//Download progress
xhr.addEventListener("progress", function(evt){
console.log([evt.lengthComputable, evt.loaded, evt.total]);
if (evt.lengthComputable) {
var percentComplete = (evt.loaded / evt.total) * 100;
}
}, false);
return xhr;
}
谢谢你的帮助:) 因为php文件是动态的,所以需要设置正确的内容长度头:
<?
ob_start();
/* your code here */
$length = ob_get_length();
header('Content-Length: '.$length."\r\n");
header('Accept-Ranges: bytes'."\r\n");
ob_end_flush();
始终在php脚本末尾使用这些行来正确确定长度,并输出htmls和数据(如果有$length=ob_get_length());标题('Content-Length:'.$Length.\r\n');标头('接受范围:字节'。“\r\n”);ob_end_flush();