如果我向服务器发布帖子,浏览器的开发人员控制台会将其丢弃:'http-client.js:28 POST http://todoapp.test/api/create 500(内部服务器错误)',但如果我将其发送到与 POSTMAN 相同的 url,服务器会保存数据。
这是我的 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"可能会有所帮助,请尝试这个
关于javascript - 如何修复我的 POST 函数代码可以与服务器通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58638863/