[英]How to fix my code for the POST function can communicate to the server?
If I make a post to the server the browser's developer console throw it away that: 'http-client.js:28 POST http://todoapp.test/api/create 500 (Internal Server Error)', but if I send it to the same url with POSTMAN, the server save the data.如果我在服务器上发帖,浏览器的开发者控制台就会把它扔掉:'http-client.js:28 POST http://todoapp.test/api/create 500 (Internal Server Error)',但如果我发送它将 url 与 POSTMAN 相同,服务器保存数据。
Here is my POST function:这是我的帖子 function:
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); }); }
Where is the problem on the server or on my code?服务器或我的代码上的问题在哪里? And how can I fix that?
我该如何解决?
I think you should set contentType: "application/json" might be help please try this我认为你应该设置 contentType: "application/json" 可能会有所帮助请试试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.