javascript POST 到 Express 的数组数据被解析为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5927742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST array data to Express gets parsed out as JSON
提问by JMWhittaker
My app is Node.js using Express.
我的应用程序是使用 Express 的 Node.js。
Sending this test data from my client using jQuery POST:
使用 jQuery POST 从我的客户端发送此测试数据:
{
title: 'hello',
notes: [
{title: 'note 1'},
{title: 'note 2'}
]
}
}
And this is the result in my server code:
这是我的服务器代码中的结果:
{ title: 'hello', notes: { '0': { title: 'note 1' }, '1': { title: 'note 2' } } }
I want to get the array of notes to insert into my DB as an Array. What am I missing?
我想将笔记数组作为数组插入到我的数据库中。我错过了什么?
As I can't add an answer myself for 8 hours (wtf?) BUT it does not really answer why Express.bodyParser does not parse JSON correctly
因为我自己无法在 8 小时内添加答案(wtf?)但它并没有真正回答为什么 Express.bodyParser 不能正确解析 JSON
Ok I canget it to work by using:
好的,我可以使用以下方法使其工作:
JSON.stringify ( data )
on the client then server side using
在客户端然后服务器端使用
JSON.parse( req.rawBody )
This does feel wrong and why does Express.bodyParser not parse JSON correctly?!
这确实感觉不对,为什么 Express.bodyParser 不能正确解析 JSON?!
回答by Rajat
On your client:
在您的客户端上:
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
url: '/endpoint'
});
On your server:
在您的服务器上:
console.log('POST: ',req.body);
The problem is jQuery mucks around with your data before sending it. If you set the right MIME type, than it leaves you free.
问题是 jQuery 在发送数据之前会处理您的数据。如果您设置了正确的 MIME 类型,那么您就可以自由了。
回答by Peter Lyons
Can you post your client side jQuery code, please? By default jQuery will send data as urlencoded, not JSON. See this question's answerfor the way to ensure jQuery sends real JSON data.
你能发布你的客户端 jQuery 代码吗?默认情况下,jQuery 会以 urlencoded 格式发送数据,而不是 JSON。有关确保 jQuery 发送真实 JSON 数据的方法,请参阅此问题的答案。
FYI the express/connect bodyParser middlewaresimply uses JSON.parse to parse JSON (and qs.parse to parse urlencoded data). I don't think there are any glaring bugs in those code. Thus I think you should double-check the data you are sending from the browser.
仅供参考,express/connect bodyParser 中间件仅使用 JSON.parse 来解析 JSON(并使用 qs.parse 来解析 urlencoded 数据)。我认为这些代码中没有任何明显的错误。因此,我认为您应该仔细检查从浏览器发送的数据。
回答by Josiah
I came across this old question when looking for some other nodejs stuff.
我在寻找其他 nodejs 东西时遇到了这个老问题。
It is a common misconception that jQuery.ajax()
functions send data using JSON. Data is sent by jQuery as POST data and not a JSON string. As such all data types (including the numbers in your array) are sent as strings.
jQuery.ajax()
函数使用 JSON 发送数据是一种常见的误解。数据由 jQuery 作为 POST 数据而不是 JSON 字符串发送。因此,所有数据类型(包括数组中的数字)都作为字符串发送。
This means that express parses the 'array' keys as a string, and since an array can't have a string key in javascript without being an object, it is cast to an object.
这意味着 express 将“数组”键解析为字符串,并且由于数组在 javascript 中不能有字符串键而不是对象,因此它被强制转换为对象。
回答by Tejs
It all makes sense then; you can use Express.bodyParser
to get a result like that, or you can use JSON.parse
or even eval('(' + myPostedData + ')')
to get a result object without the indexes.
那么这一切都是有道理的;您可以使用Express.bodyParser
来获得这样的结果,或者您可以使用JSON.parse
甚至eval('(' + myPostedData + ')')
获得没有索引的结果对象。
With your current setup, all you need to do is:
使用您当前的设置,您需要做的就是:
for(var j = 0; j < myVariable.notes.length; j++)
{
var currentNode = myVariable.notes[j];
//currentode.title should be 'note 1' for j = 0, etc
}