Chrome上的XMLHttpRequest onprogress总数始终为0 [英] XMLHttpRequest onprogress total is always 0 on Chrome

查看:75
本文介绍了Chrome上的XMLHttpRequest onprogress总数始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示进度条,然后再在同一网站中从一个页面导航到另一个页面.

I am trying to show a progress bar before navigating from one page to another within the same website.

我的函数将updateProgress函数绑定到XMLHttpRequest onprogress事件,并将用户重定向到(xhr.readyState == 4&& xhr.status == 200)上的新页面除了Chrome显示总计"为零(这不会使进度条正常运行)外,它似乎工作正常.我的代码在下面.

My function binds an updateProgress function to XMLHttpRequest onprogress event and it redirects user to a new page on (xhr.readyState == 4 && xhr.status == 200) It seems working fine except that Chrome shows "total" as zero which won't let the progress bar function properly. My code is below.

提前谢谢...

$('.ajaxNavi').click(function (e) {
  e.preventDefault();
  var url = $(this).attr('href');
  var xhr = new XMLHttpRequest();
  xhr.onprogress = updateProgress;
  xhr.onreadystatechange = function () { 
    if (xhr.readyState == 4 && xhr.status == 200)
    // REDIRECT HERE
    }
  });
  xhr.open("GET", url, true);
  xhr.setRequestHeader("Content-Type", "text/html");
  xhr.send();
});

function updateProgress(e) {
  console.log(e.loaded + '    ' + e.total);
}

推荐答案

晚了两年,但是这个问题似乎有很多见解,应该回答.

Two years late, but this question seems to have a lot of views and should probably be answered.

根据问题,一个压缩的响应将在Chrome中始终将lengthComputable设置为false.这很有道理,因为您可能知道有多少压缩数据正在传输,但是您不知道压缩数据将扩展到多少.

According to this question a gzipped response will always have lengthComputable set to false in Chrome. This makes a lot of sense because you may know how much more compressed data is coming down the pipe, but you can't have any idea how much that compressed data will expand to.

这个解决方案对我来说似乎是一个很好的解决方案.

This solution seems like a good one to me.

这篇关于Chrome上的XMLHttpRequest onprogress总数始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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