新的XMLHttpRequest()在chrome中不起作用?

时间:2012-07-30 11:58:23

标签: google-chrome xmlhttprequest

我知道这听起来很傻......它应该在那里工作......但是:

function fetch(id){
    var fetch = new XMLHttpRequest();
    fetch.open("GET", "/upload/status?X-Progress-ID=" + id);
    fetch.onreadystatechange = function () {
      console.log(fetch.readyState);
    }
    fetch.send(null);
}

此代码适用于IE浏览器,在Firefox中...我得到所有4个状态返回...但是当我在chrome中尝试它时我绝对没有...我完全不知道为什么......

我认为你已经猜到了这个问题,为什么它不起作用?

修改

稍微更改了代码......正如您可能已经猜到的那样,我使用nginx上传进度模块,并且每隔一秒调用此函数以获取上传文件的进度...

编辑1

我试图重写$ .ajax(),我基本上得到了相同的结果..即,firefox工作,而chrome只是给了我什么。以下是该页面的完整代码:

<!doctype html>
<html lang="ru">
<head>
  <meta charset="utf-8">
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
  <script src="static/js/jquery-ui-1.8.21.custom.min.js" type="text/javascript"></script>
  <script>
    function add() {
      if (parseInt(document.getElementById('count').getAttribute('value')) < 8) {
        var input = document.createElement('input');
        input.setAttribute('type','file');
        input.setAttribute('multiple','');
        input.setAttribute('name','file[]');
        document.getElementById('multiple').appendChild(input);
        document.getElementById('multiple').appendChild(document.createElement('br'));
        document.getElementById('count').setAttribute('value',parseInt(document.getElementById('count').getAttribute('value'))+1);
      }
      else {
        alert('Можно загрузить не более 8 файлов за раз.');
      }
    }
    function progress() {
      var ms = new Date().getTime() / 1000;
      rq = 0;
      id = "";
      for (i = 0; i < 32; i++) {
        id += Math.floor(Math.random() * 16).toString(16);
      }
      document.getElementById('upload').action = "/upload/share?X-Progress-ID=" + id;
//      console.log("/upload/share?X-Progress-ID=" + id);
      document.getElementById('status').style.display = 'block'
      interval = window.setInterval(function () { fetch(id, ms); }, 1000);
      return true;
    }
    function fetch(id, ms) {
//      console.log("/upload/status?X-Progress-ID=" + id);


      $.ajax({
        type: "GET",
        url: "/upload/status?X-Progress-ID="+id,
        dataType: 'text',
        complete: function(upload) {
          console.log('test');
          alert('test');
        }
      });

//      var fetch = new XMLHttpRequest();
//      fetch.open("GET", "/upload/status", 1);
//      fetch.setRequestHeader("X-Progress-ID", id);
//      fetch.open("GET", "/upload/status?X-Progress-ID=" + id);
//      fetch.setRequestHeader("X-Progress-ID", id);

//      fetch.onreadystatechange = function () {
//        console.log('rorororo');
//        console.log(fetch.readyState);
//        if (fetch.readyState == 3) {
//          console.log(fetch.responseText);
//
////          if (fetch.status == 200) {
//
//            var now = new Date().getTime() / 1000;
//            var upload = eval(fetch.responseText);
//
//            if (upload.state == 'uploading') {
//              var diff = upload.size - upload.received;
//              var rate = upload.received / upload.size;
//              var elapsed = now - ms;
//              var speed = upload.received - rq; rq = upload.received;
//              var remaining = (upload.size - upload.received) / speed;
//              var uReceived = parseInt(upload.received) + ' bytes';
//              var uDiff = parseInt(diff) + ' bytes';
//              var tTotal = parseInt(elapsed + remaining) + ' secs';
//              var tElapsed = parseInt(elapsed) + ' secs';
//              var tRemaining = parseInt(remaining) + ' secs';
//              var percent = Math.round(100*rate) + '%';
//              var uSpeed = speed + ' bytes/sec';
//              document.getElementById('length').firstChild.nodeValue = parseInt(upload.size) + ' bytes';
//              document.getElementById('sent').firstChild.nodeValue = uReceived;
//              document.getElementById('offset').firstChild.nodeValue = uDiff;
//              document.getElementById('total').firstChild.nodeValue = tTotal;
//              document.getElementById('elapsed').firstChild.nodeValue = tElapsed;
//              document.getElementById('remaining').firstChild.nodeValue = tRemaining;
//              document.getElementById('speed').firstChild.nodeValue = uSpeed;
//              document.getElementById('bar').firstChild.nodeValue = percent;
//              document.getElementById('bar').style.width = percent
//            }
//            else {
//              window.clearTimeout(interval);
//            }
////          }
//        }

//      }
//      fetch.send(null);
    }
  </script>
</head>
<body>
<form method="post" enctype="multipart/form-data" id="upload" onsubmit="progress();">
  <input type="hidden" id="count" value="1" />
  <input type="hidden" value="GOBLEBOELBOE" name="secret">
  <div id="multiple">
    <input type="file" name="file[]" multiple /><br>
  </div>
  <input type="submit">
  <a href="#" onclick="add();">add();</a>
</form>
<div id="status" style="display: none;">
  <table width="100%">
    <tr><th></th><th>загрузка</th><th>осталось</th><th>всего</th></tr>
    <tr><td>время:</td><td id="elapsed">∞</td><td id="remaining">∞</td><td id="total">∞</td></tr>
    <tr><td>размер:</td><td id="sent">0 b</td><td id="offset">0 b</td><td id="length">0 b</td></tr>
    <tr><td>скорость:</td><td id="speed">n/a</td></tr>
  </table>
  <div style="border: 1px solid #c0c0c0;">
    <div style="background: #c0c0c0; width: 0%; text-align: right;" id="bar">0%</div>
  </div>
  <a href="#" onclick="if (confirm('Вы точно хотите отменить загрузку?')) window.location = '/'" id="cancel">cancel_upload();</a>
</div>
</body>
</html>

答案: http://code.google.com/p/chromium/issues/detail?id=45196

2 个答案:

答案 0 :(得分:0)

你应该添加这个

fetch.send(null)
  

开()

     

初始化请求。此方法将从JavaScript代码中使用;要从本机代码初始化请求,请改用openRequest()。

     

的send()

     

发送请求。如果请求是异步的(这是默认值),则此方法会在发送请求后立即返回。如果请求是同步的,则在响应到达之前,此方法不会返回。

引自MDN

答案 1 :(得分:0)