【问题标题】:node.js request POST array "first argument must be string or buffer"node.js 请求 POST 数组“第一个参数必须是字符串或缓冲区”
【发布时间】:2016-07-17 09:03:43
【问题描述】:

我正在尝试将一个数组从一个 node.js 进程(客户端)发送到另一个(服务器)。

我在“客户端”node.js 上的代码:

var express        = require('express');
var app            = express();
var config = require('./config');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var request     = require('request');

app.set('port', process.env.PORT || 3009);
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded

app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT

var arr = [{
        date : "2016/1/26",
        count: 6
    },
               {
        date : "2016/1/27",
        count: 0
    },
    {
        date : "2016/1/28",
        count: 0
    },
    {
        date : "2016/1/29",
        count: 0
    },
    {
        date : "2016/1/30",
        count: 0
    },
    {
        date : "2016/1/31",
        count: 2
    },
    {
        date : "2016/2/1",
        count: 0
    },
    {
        date : "2016/2/2",
        count: 4
    }];


       request.post({
            uri: config.URL,
            headers: {
                'Content-Type':'application/json;charset=UTF-8',
                'Accept-Encoding':'gzip, deflate',
                'X-Requested-With': 'XMLHttpRequest',
                'Accept':'application/json, text/plain, */*',
                'User-Agent': 'UserAgent' 
            },
            body: arr
            }, function(err, res, body){

                  //whatever
            });

在服务器端,我只收到信息并控制台记录它。

启动代码时,我进入客户端:TypeError('first argument must be a string, Array, or Buffer');

为什么我不能发送数组?我发誓我已经做了 1000 次......


如果我像这样在客户端中对数组进行字符串化:body: JSON.stringify(arr),而在服务器中我尝试将其解析回这样的数组:var data = JSON.parse(req.body); 在解析数据时,我在服务器中收到以下错误:

SyntaxError: Unexpected token o
    at Object.parse (native)
    at exports.uploadReads (C:\node\stockare2\server\companys\companys.controller.js:628:21)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13)
    at uploadUser (C:\node\stockare2\server\companys\companys.routes.js:117:7)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\node\stockare2\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (C:\node\stockare2\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at C:\node\stockare2\node_modules\express\lib\router\index.js:277:22
    at Function.process_params (C:\node\stockare2\node_modules\express\lib\router\index.js:330:12)
    at next (C:\node\stockare2\node_modules\express\lib\router\index.js:271:10)
    at serveStatic (C:\node\stockare2\node_modules\express\node_modules\serve-static\index.js:74:16)
    at Layer.handle [as handle_request] (C:\node\stockare2\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\node\stockare2\node_modules\express\lib\router\index.js:312:13)
    at C:\node\stockare2\node_modules\express\lib\router\index.js:280:7

如果我在尝试解析数据之前 console.log req.body,我可以看到数据的格式很奇怪:

{ '{"date":"2016/1/26......

【问题讨论】:

    标签: javascript arrays json node.js request


    【解决方案1】:

    尝试在请求中添加选项json: true

    request.post({
            uri: config.URL,
            headers: {
                'Content-Type':'application/json;charset=UTF-8',
                'Accept-Encoding':'gzip, deflate',
                'X-Requested-With': 'XMLHttpRequest',
                'Accept':'application/json, text/plain, */*',
                'User-Agent': 'UserAgent' 
            },
            json: true,
            body: arr
            }, function(err, res, body){
    

    【讨论】:

      【解决方案2】:

      使用var data = eval(req.body)转换成javascript对象。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      相关资源
      最近更新 更多