我正在尝试使用XMLHttpRequest将文件发送到我的后端。在后端我使用弹簧安全。前端使用Angular 2.问题是XMLHttpRequest发送一个完全不同的JSESSIONID Cookie(总是相同的)并被拒绝访问。我使用Angular' http.post
的其他请求发送了正确的JSESSIONID。错误的JSESSIONID似乎无处不在,我无法确定原因。
这是我的XMLHttpRequest:
postFormData() {
return fromPromise(new Promise((resolve, reject) => {
let formData: any = new FormData()
let xhr = new XMLHttpRequest()
formData.append("file", this.file, this.file.name);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(xhr.response)
} else {
reject(xhr.response)
}
}};
console.log("sending stuff")
//xhr.setRequestHeader();
xhr.open("POST", 'http://localhost:8080/import/loadsheets', true);
//xhr.setRequestHeader("withCredentials","true");
xhr.withCredentials = true;
//xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("Access-Control-Allow-Credentials", "true");
//let sess = this.cookieService.get("cookie");
//console.log('Cookie: ', sess);
//xhr.setRequestHeader('Cookie', sess);
xhr.send(formData)
}));
}
问题是什么?提前谢谢!