我正在使用 XHR 上传一个在 FF 中运行良好但在 Chrome 中失败的文件。
抛出一个错误,提示 Upload failed: 0
这意味着 xhr.status 返回为 0
- 我不确定这是什么意思?没有记录其他状态。
//Check if we have XHR / File support
if (typeof File != "undefined" && typeof (new XMLHttpRequest()).upload != "undefined")
{
var xhr = new XMLHttpRequest();
xhr.upload.onprogress = function(e){
if (e.lengthComputable){
uploadStarted = true;
var loaded = (e.loaded / e.total) * 100;
ShowProgress(loaded);
}
};
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status == 200){
uploadComplete();
} else {
alert("Upload failed: " + xhr.status);
}
console.log("status",xhr.status);
}
};
var formElement = document.getElementById("configForm");
xhr.open("POST", $("#configForm").attr('action') , true);
xhr.send(new FormData(formElement));
}
最佳答案
xhr.status == 0
表示有 some network error .但规范并未说明如何找出错误所在。
我建议将整个 xhr
结构记录到控制台 (console.log(xhr)
)。也许其他字段之一包含一些有值(value)的数据。
如果失败,请检查服务器的错误日志。可能服务器端出了点问题。
关于javascript - 在 Chrome 中通过 XHR 上传 HTML5 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6777571/