0

我需要按照CORS规范将文件上传到服务器javascript

互联网上有很多示例,实际上一切正常(文件已上传),但最后触发的事件是一个错误。

如果我查看目标,XMLHttpRequest则以readyState=4. 根据规范,这是

DONE: The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

source http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-readystate

status总是,0但文件确实被上传,进度正常,服务器返回一个201 CREATED.

一段代码:

var fd = new FormData(document.getElementById('viForm'));
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", submitAddress);
xhr.setRequestHeader('X-Auth-Token', token);
xhr.send(fd);


function uploadFailed(e) { console.log(e); }
4

1 回答 1

0

问题是,虽然我将服务器设置为在请求中发回适当的标头,但OPTIONS我并没有Access-Control-Allow-Origin在实际的POST.

xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
于 2013-05-07T11:16:03.403 回答