如何通过邮差发送JSON到NodeJS Express服务器?

我试图从Chrome应用程序“邮差”通过POST发送JSON对象。

如果我将头设置为'Content-Type: text/plain' ,那么另一边的输出是一个空对象:

 {} 

如果我将头设置为Content-Type: application/json ,我收到以下响应…

'错误:无效的JSON'。

关键是设定ingredients ,其值设定为:

 { ["name" : "num1", "quantity" : "5"], ["name" : "num2", "quantity" : "2"], [ "name" : "num3", "quantity" : "8"]} 

我在这里看到它:

 router.post('/add', jsonParser, function( req, res ) { if (!req.body) return res.sendStatus(400); console.log( req.body ); }); 

首先你需要根据你的模型devise数据。 使用http://jsoneditoronline.org/将Json格式化为语法错误

第1步: – 设置URL参数(如果你有)

第2步: – 设置标准的Http头

例如: – 1)。 内容types:应用程序/ JSON 2)。 接受:application / json

然后根据您的要求使用http CRUD操作

您需要发送数据到POST和PUT操作Get – 可以从api获取json

后例如: – select邮政与API你看到3个选项表单 – 数据X – WWWforms – urlencoded原始select选项“原始”和粘贴你的JSON内容

祝你好运 !!

你的JSON无效。 重构到一个能parsing正确的东西,继续工作。 下面的例子应该正常工作,你的对象数组存储在ingredients

 { "ingredients": [{ "name": "num1", "quantity": "5" }, { "name": "num2", "quantity": "2" }, { "name": "num3", "quantity": "8" }] } 

你可以随意切换,只要确保parsing就行。 JSONLint将是你的朋友。 即使是一个没有命名标识符的普通数组也可以工作,而我越是看它,就好像你只是简单地使用了{}[]语法。

 [{"name": "num1", "quantity": "5"}, {"name": "num2","quantity": "2"}, {"name": "num3","quantity": "8"}]