我在 Node 中尝试向端点发送 http 调用。我没有得到任何回应,而且我确定我设置不正确。我的请求适用于 postman 。任何帮助都会很棒!
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
curlUpdate = function curlUpdate() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "www.example.com", false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send({
FirstName: "john",
LastName: "doe",
MerchantSessionID: "817281271",
DateOfBirth: "12,24,1985",
ProductTypeID: "xxxxxxxxx"
});
console.log(xhr.responseText);
};
最佳答案
您只需要用 JSON.stringify()
包装您的对象。
所以你的 xhr.send 看起来像:
xhr.send(JSON.stringify({
FirstName: "john",
LastName: "doe",
MerchantSessionID: "817281271",
DateOfBirth: "12,24,1985",
ProductTypeID: "xxxxxxxxx"
}));
附言不要忘记将地址(现在是代码中的 www.example.com
)指向正确的端点。
关于javascript - 类型错误 "string"must be a string, Buffer, or ArrayBuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47241766/