javascript - 请求 POST 修改对象?

标签 javascript node.js request

我正在尝试使用像这样的帖子来通过 Request 发布一个对象 ->

function postData(data, cb) {
    request.post({
        url: 'http://localhost:3001/datastream',
        form: data,
    }, (err, httpResponse, body) => {
        cb(body);
    });
}

该对象看起来像这样:

{
    "tblPartsReport": {
        "valid": true,
        "message": "Execute SQL: SELECT * FROM tblPartsReport WHERE ID = (SELECT MAX(ID) FROM tblPartsReport); success !",
        "records": [{
            "ResourceID": 61,
            "TimeStamp": "2017-04-04T05:52:19Z",
            "PNo": 0,
            "ErrorID": 0,
            "ID": 10174
        }]
    },
    "tblMachineReport": {
        "valid": true,
        "message": "Execute SQL: SELECT * FROM tblMachineReport WHERE ID = (SELECT MAX(ID) FROM tblMachineReport); success !",
        "records": [{
            "ResourceID": 61,
            "TimeStamp": "2017-04-04T05:52:19Z",
            "AutomaticMode": true,
            "ManualMode": false,
            "Busy": false,
            "Reset": false,
            "ErrorL0": false,
            "ErrorL1": false,
            "ErrorL2": false,
            "ID": 26562
        }]
    }
}

该对象是有效且正常的,但发布后在另一边看起来像这样:

console.log(req.body);

{
    'tblMachineReport[valid]': 'true',
    'tblMachineReport[message]': 'Execute SQL: SELECT * FROM tblMachineReport WHERE ID = (SELECT MAX(ID) FROM tblMachineReport); success !',
    'tblMachineReport[records][0][ResourceID]': '61',
    'tblMachineReport[records][0][TimeStamp]': '2017-04-04T05:52:19Z',
    'tblMachineReport[records][0][AutomaticMode]': 'true',
    'tblMachineReport[records][0][ManualMode]': 'false',
    'tblMachineReport[records][0][Busy]': 'false',
    'tblMachineReport[records][0][Reset]': 'false',
    'tblMachineReport[records][0][ErrorL0]': 'false',
    'tblMachineReport[records][0][ErrorL1]': 'false',
    'tblMachineReport[records][0][ErrorL2]': 'false',
    'tblMachineReport[records][0][ID]': '26562',
    'tblPartsReport[valid]': 'true',
    'tblPartsReport[message]': 'Execute SQL: SELECT * FROM tblPartsReport WHERE ID = (SELECT MAX(ID) FROM tblPartsReport); success !',
    'tblPartsReport[records][0][ResourceID]': '61',
    'tblPartsReport[records][0][TimeStamp]': '2017-04-04T05:52:19Z',
    'tblPartsReport[records][0][PNo]': '0',
    'tblPartsReport[records][0][ErrorID]': '0',
    'tblPartsReport[records][0][ID]': '10174'
}

知道为什么会发生这种情况吗?我也尝试过 Axios,但无法让帖子正常工作。我只想发布一个常规对象。通常我一直在使用 jQuery AJAX。

编辑: 这是正确的方法:)

function postData(data, cb) {
    request.post({
        url: 'http://localhost:3001/datastream',
        json: true,
        body: data,
    }, (err, httpResponse, body) => {
        cb(body);
    });
}

最佳答案

我假设您使用 Node 模块请求。 您必须使用 json 选项而不是 form 选项。 将 json 设置为 true 并将数据放在正文中而不是表单中。

body - entity body for PATCH, POST and PUT requests. Must be a Buffer, String or ReadStream. If json is true, then body must be a JSON-serializable object.

form - when passed an object or a querystring, this sets body to a querystring representation of value, and adds Content-type: application/x-www-form-urlencoded header. When passed no options, a FormData instance is returned (and is piped to request). See "Forms" section above.

https://github.com/request/request#requestoptions-callback

据我所知,您在帖子中包含的输出格式为 application/x-www-form-urlencoded PHP 和其他纯服务器端语言更喜欢这种格式.

关于javascript - 请求 POST 修改对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43317907/

相关文章:

javascript - react native : State seems to retain previous state even after reload on the simulator

php - 使用 Visual Studio 调试 PHP 应用程序中的 JavaScript

javascript - JSHandles 只能在创建它们的上下文中进行评估/找不到具有指定 ID 的上下文

javascript - 构建需要很长时间响应的 API 的最佳实践是什么?

php - 如何将请求堆栈注入(inject) Twig 扩展

javascript - 如何在弹出窗口中添加下一个上一个按钮?

javascript - "click"监听器多次执行

javascript - 将带有resolve和reject的promise转换为异步函数的正确方法(puppeteer)

javascript - NodeJS 获取流式文件下载的字节数

node.js - 无法使用 Node 请求模块在 Linux 上读取 LinkedIn 内容