javascript - 类型错误 "string"must be a string, Buffer, or ArrayBuffer

标签 javascript node.js xmlhttprequest

我在 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/

相关文章:

javascript - 在字符串末尾增加一个数字

node.js - Node.js将目录中的文件名与文本文件中的文件名进行比较

javascript - 主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响

javascript - 在 IE7 和 IE8 中为 IXMLHttpRequest 对象赋值时出现异常

javascript - 一个接一个地调用脚本

一旦涉及代理,Javascript 将无法正常运行

Node.js:我应该在错误时丢弃请求吗?

javascript - OS X Javascript Widget 中 XMLHttpRequest 的空响应

jquery - 未捕获的类型错误 : Cannot call method 'noConflict' of undefined

node.js - 错误: PDFDocument: stream must have data