我有一些我用来上传文件异步的代码 - 效果很好:
var form = document.getElementById('form-id');
var formData = new FormData(form);
var fileInput = document.getElementById('file');
var file = fileInput.files[0];
formData.append('our-file', file);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('loadstart', onloadstartHandler, false);
xhr.upload.addEventListener('progress', onprogressHandler, false);
xhr.upload.addEventListener('load', onloadHandler, false);
xhr.addEventListener('readystatechange', onreadystatechangeHandler, false);
xhr.open('POST', 'http://www.server1.com/upload.php', true);
xhr.send(formData);
问题是如果我想发布到备用域(即:www.server2.com/upload.php)它不起作用。它仅在我发布到脚本所在的同一域时才有效。