ajax xhr lengthComputable 使用 php 文件返回 false [英] ajax xhr lengthComputable return false with php file

查看:31
本文介绍了ajax xhr lengthComputable 使用 php 文件返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XMLHttpRequest 执行 ajax 请求,以显示请求的进度.它适用于 html 文件,但 evt.lengthComputable 使用 php 文件返回 false.

i'm doing an ajax request with a XMLHttpRequest in order to show the progress of the request. It's working great with an html file but evt.lengthComputable return false with a php file.

我的 php 文件是 utf-8 编码的,没有什么特别的.

My php file is encoded in utf-8 and contain nothing special.

                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文件是动态的,需要设置正确的Content-Length头:

Because php file are dynamic, you need to set a correct Content-Length header:

<?
ob_start();
/* your code here */

$length = ob_get_length();
header('Content-Length: '.$length."\r\n");
header('Accept-Ranges: bytes'."\r\n");
ob_end_flush(); 

这篇关于ajax xhr lengthComputable 使用 php 文件返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆