关于JS跨域上传文件的问题
JS代码:
var url = "http://www.xxxx.com/xxxx/api/public/a.ashx";
var form = new FormData();
for (var i = 0; i < filecount; i++) {
var fileObj = document.getElementById('upload').files[i];
form.append('myfile', fileObj);
form.append('filename', fileObj.name);
}
form.append("folder", "../../../uploadfiles/factory/");
form.append("userId", userid);
let xhr = new XMLHttpRequest();
xhr.open("post", url, true);
// xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.withCredentials = false;
xhr.onload = uploadComplete; //请求完成
xhr.onerror = uploadFailed; //请求失败
xhr.upload.onprogress = progressFunction;//进度
xhr.send(form);
报如下错误:
Failed to load http://www.xxxx.com/xxx/api/public/xxxx.ashx: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.xxx.com' is therefore not allowed access.
但是我把 xhr.upload.onprogress = progressFunction;//进度 这句进度条代码删掉就不会报错了,不知道是什么原因。
看哪位遇到过,谢谢。