- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将一个数组从一个 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
});
在服务器端,我只接收信息并在 console.log 中记录它。
启动代码时,我进入客户端: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......
最佳答案
尝试在请求中添加选项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){
关于javascript - node.js 请求 POST 数组 "first argument must be string or buffer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290552/
我期望 new Buffer(buffer.toString()) 始终是逐字节相等的。但是,我遇到的情况并非如此。 首先,这是一个真实的案例: var buf1 = new Buffer(32);
我有用于记录数据的 Protocol Buffer 。 message Message { required double val1 = 1; optional int val2 =
请注意以下简单程序(基于 protobuf-net 项目 v1 wiki 中的示例): using System.Collections.Generic; using System.Diagnosti
在 Protocol Buffer 中,有没有办法让消息包含嵌套消息的集合?例如,消息主管可能有一个员工集合以及主管的姓名和部门。 最佳答案 是的。您使用 repeated领域; message Em
我想知道 Protocol Buffer 在解析流时如何处理损坏的数据。有没有办法知道数据是否已损坏。 Protocol Buffer 是否提供任何内置的数据完整性检查机制? 谢谢, 最佳答案 没有任
Protocol Buffer 如何处理类型版本控制? 例如,当我需要随时间更改类型定义时?就像添加和删除字段一样。 最佳答案 Google 设计的 protobuf 对版本控制非常宽容: 意外数据要
我尝试阅读 Protobuf 文档,但无法想象它可以用于许多用例。我想知道一些实际的 Protocol Buffer 性能改进用例。 谢谢 最佳答案 Protocol buffers 是一个序列化库,
给定 Protocol Buffer 模式和一些数据, Protocol Buffer 序列化是否跨库和语言具有确定性? 基本上,无论使用什么库,我是否可以保证相同的数据总是以相同的方式(直到字节)序
我正在使用一个示例 UWP C++/CX 程序,该程序创建两个 UDP 网络通信线程,它们使用 Windows::Storage::Streams::DataWriter 相互发送数据。和 Windo
我正在使用以下代码 int lenSend = odl->ByteSize(); char* buf = (char *)malloc(lenSend); odl->SerializeToArray(
Protocol Buffer 文档警告说...... You should never add behaviour to the generated classes by inheriting fr
我有一个定义如下的原型(prototype)模式, message User { int64 id = 1; bool email_subscribed = 2; bool sms_
我试图了解 Protocol Buffer 编码方法,将消息转换为二进制(或十六进制)格式时,我无法理解嵌入消息的编码方式。 我猜可能和内存地址有关,但我找不到准确的关系。 这是我所做的。 第 1 步
我需要序列化和反序列化一系列与字节流之间的 Protocol Buffer 消息。有一些预先确定的消息类型。编码类型信息的推荐方法是什么,以便我的应用程序可以知道它应该读取哪种类型? 最佳答案 最常见
与GSON相比, Protocol Buffer (protobuf)的优缺点是什么? 在什么情况下,protobuf比GSON更合适? 对于一个非常笼统的问题,我感到抱歉。 最佳答案 json(通过
message Person { required Empid = 1 [default = 100]; required string name = 2 [default = "Raju"]
我正在研究一个小型设备,该设备具有相当大的一组配置参数(~100 KB),这些参数是从 PC 软件生成的。过去,我们将参数存储在二进制文件中并将它们加载到数据结构中。维护有点烦人(不同的语言,确保结构
来自Encoding - Protocol Buffers - Google Code上的“签名类型”: ZigZag encoding maps signed integers to unsigne
我正在使用 Protocol Buffer ,一切正常。除了我不明白的事实 - 为什么我需要 proto 中的编号标签文件 : message SearchRequest { required s
Protocol Buffer 的吸引人的功能之一是它允许您扩展消息定义,而不会破坏使用较旧定义的代码。对于枚举according to the documentation: a field with
我是一名优秀的程序员,十分优秀!