如何修复我的 POST 函数代码可以与服务器通信?
How to fix my code for the POST function can communicate to the server?
如果我向服务器发送 post,浏览器的开发人员控制台会将其丢弃:'http-client.js:28 POST http://todoapp.test/api/create 500 (Internal Server Error)',但如果我将其发送到与 [=22= 相同的 url ]MAN,服务器保存数据。
这是我的 POST 函数:
export class HttpClient {
constructor(url) {
this.url = url;
this.xhr = new XMLHttpRequest();
this.xhr.onloadend = (event) => {
return this.xhr.response;
}
}
setHeader(header) {
this.xhr.setRequestHeader(header.name, header.value);
}
post(async, data, header) {
return new Promise((resolve, reject) => {
this.xhr.open('POST', this.url + 'create', async);
this.xhr.setRequestHeader(header.name, header.value);
this.xhr.send(data);
resolve(this.xhr.response);
});
}
服务器或我的代码哪里有问题?
我该如何解决?
我认为你应该设置 contentType:"application/json" 可能会有帮助,请试试这个
如果我向服务器发送 post,浏览器的开发人员控制台会将其丢弃:'http-client.js:28 POST http://todoapp.test/api/create 500 (Internal Server Error)',但如果我将其发送到与 [=22= 相同的 url ]MAN,服务器保存数据。
这是我的 POST 函数:
export class HttpClient { constructor(url) { this.url = url; this.xhr = new XMLHttpRequest(); this.xhr.onloadend = (event) => { return this.xhr.response; } } setHeader(header) { this.xhr.setRequestHeader(header.name, header.value); } post(async, data, header) { return new Promise((resolve, reject) => { this.xhr.open('POST', this.url + 'create', async); this.xhr.setRequestHeader(header.name, header.value); this.xhr.send(data); resolve(this.xhr.response); }); }
服务器或我的代码哪里有问题? 我该如何解决?
我认为你应该设置 contentType:"application/json" 可能会有帮助,请试试这个