Google chrome 新的XMLHttpRequest()没有';你不能在铬上工作吗?
我知道这听起来有点傻。。。它应该在那里工作。。。但是:Google chrome 新的XMLHttpRequest()没有';你不能在铬上工作吗?,google-chrome,xmlhttprequest,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(
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 upload progress模块,这个函数被调用为everysecond以获取上传文件的进度
编辑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>
函数add(){
if(parseInt(document.getElementById('count').getAttribute('value'))小于8){
var input=document.createElement('input');
setAttribute('type','file');
input.setAttribute('multiple','');
setAttribute('name','file[]);
document.getElementById('multiple').appendChild(输入);
document.getElementById('multiple').appendChild(document.createElement('br'));
document.getElementById('count').setAttribute('value',parseInt(document.getElementById('count').getAttribute('value'))+1);
}
否则{
警报('Мжзззззззззззззззззз');
}
}
功能进展(){
var ms=new Date().getTime()/1000;
rq=0;
id=“”;
对于(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(函数(){fetch(id,ms);},1000);
返回true;
}
函数获取(id,毫秒){
//console.log(“/upload/status?X-Progress-ID=“+ID”);
$.ajax({
键入:“获取”,
url:“/upload/status?X-Progress-ID=“+ID,
数据类型:“文本”,
完成:功能(上传){
console.log('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=函数(){
//console.log('rorororo');
//console.log(fetch.readyState);
//if(fetch.readyState==3){
//console.log(fetch.responseText);
//
////如果(fetch.status==200){
//
//var now=new Date().getTime()/1000;
//var upload=eval(fetch.responseText);
//
//如果(upload.state==“upload”){
//var diff=upload.size-upload.received;
//var rate=upload.received/upload.size;
//var经过=现在-毫秒;
//var speed=upload.received-rq;rq=upload.received;
//var剩余=(upload.size-upload.received)/速度;
//var uReceived=parseInt(upload.received)+“bytes”;
//var uDiff=parseInt(diff)+“字节”;
//var ttottal=parseInt(已用时间+剩余时间)+“秒”;
//var tElapsed=parseInt(经过时间)+“秒”;
//var tRemaining=parseInt(剩余)+“秒”;
//变量百分比=数学四舍五入(100*比率)+'%';
//var uSpeed=速度+字节/秒;
//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=TTottal;
//document.getElementById('eassed').firstChild.nodeValue=tElapsed;
//document.getElementById(‘剩余’).firstChild.nodeValue=tRemaining;
//document.getElementById('speed').firstChild.nodeValue=uSpeed;
//document.getElementById('bar').firstChild.nodeValue=百分比;
//document.getElementById('bar')。style.width=百分比
// }
//否则{
//窗口清除超时(间隔);
// }
//// }
// }
// }
//fetch.send(空);
}
загрузкаосталосьвсего
время:∞∞∞
аззззааа:0 b0 b0 b
бббббб:不适用
0%
回答:您应该添加此选项
fetch.send(null)
开()
初始化请求。此方法将从JavaScript代码中使用;要从本机代码初始化请求,请改用openRequest()
发送()
发送请求。如果请求是异步的(这是默认情况),则该方法会在发送请求后立即返回。如果请求是同步的,则此方法在响应到达之前不会返回
引用自显然chrome有很多缺陷。
控制台中是否有异常。看,我编辑了一段代码。。。不,绝对没有例外。。。这是主要问题。。它根本不叫。。。。因为如果我在这里
fetch.onreadystatechange=function(){}
中记录一些随机字符串,那么它绝对不会产生任何结果。。。。