如何解决使用 XMLHttpRequest 但使用 Fetch 时缺少正文的 POST 错误?
我有以下发布功能
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
function post(url,params,callback,method='POST') {
var http = new XMLHttpRequest();
http.open(method,url,true);
http.setRequestHeader('Content-type','application/json');
http.onload = function() {
console.log(http.status) <---- 500
console.log(http.responseText) <----'ERROR: An 'ipAddr' must be provided in the request body.',callback(http);
};
console.log(JSON.stringify(params)) <--- print {"ipAddr":"175.00.00.00","params":"params_string"}
http.send(JSON.stringify(params));
}
不断收到 500,并显示 ipAddr
丢失
但我在请求正文中传递了 ipAddr
......我错过了什么?
顺便说一下,我试图到达的端点有效。确认卷曲
curl -i --header tion/json" -d '{"ipAddr": "175.00.00.00","params": "params_string"}' --request POST https://my.endpoint/run
这会返回 200 结果。
更新...
切换到使用 fetch,然后它起作用了吗?不知道为什么,相同的内容翻译过来
var fetch = require("node-fetch");
function post(url,method='POST') {
// request options
const options = {
method: 'POST',body: JSON.stringify(params),headers: {
'Content-Type': 'application/json',}
}
// send post request
fetch(url,options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。