【问题标题】:Type Error "string" must be a string, Buffer, or ArrayBuffer类型错误“字符串”必须是字符串、Buffer 或 ArrayBuffer
【发布时间】:2018-04-24 19:16:54
【问题描述】:

我在 Node 中尝试向端点发送 http 调用。我没有得到任何回应,而且我确定我的设置不正确。我的请求适用于邮递员。任何帮助都会很棒!

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);
};

【问题讨论】:

  • 我认为您应该将字符串传递给 send 而不是对象。在将对象传递给send之前尝试JSON.stringify
  • 是的,这似乎可行,谢谢! @ibrahimmahrir

标签: javascript node.js xmlhttprequest


【解决方案1】:

你只需要用JSON.stringify()包装你的对象。

所以你的 xhr.send 看起来像:

xhr.send(JSON.stringify({
        FirstName: "john",
        LastName: "doe",
        MerchantSessionID: "817281271",
        DateOfBirth: "12,24,1985",
        ProductTypeID: "xxxxxxxxx"
    }));

附:不要忘记将地址(现在在您的代码中为 www.example.com)指向正确的端点。

【讨论】:

    猜你喜欢
    • 2019-11-10
    • 2022-01-12
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多