Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax xhr LengthComputeable返回false与php文件_Php_Xmlhttprequest - Fatal编程技术网

ajax xhr LengthComputeable返回false与php文件

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

我正在使用XMLHttpRequest执行一个ajax请求,以显示请求的进度。它在html文件中工作得很好,但在php文件中evt.lengthComputeable返回false

我的php文件是用utf-8编码的,不包含任何特殊内容

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