ajax POST JSON数组到Nodejs服务器删除密钥

解决! 看到这个post底部的解决scheme…我已经被困在这个问题约2天,现在开始找我。 我想发布一个Json数组到我的节点服务器(跨域),并将其插入到云数据库。 这个问题更多的是关于以正确的格式获取JSON。 这里是我的客户端JSON和AJAX:

function stress(){ var start = new Date().getTime(); $.ajax({ type: 'POST', url: 'url/', crossDomain: true, data: JSON.stringify(products), dataType: 'json', contentType: "application/x-www-form-urlencoded; charset=UTF-8", success: function(responseData, textStatus, jqXHR) { var end = new Date().getTime(); var millis = (end - start); var duration = (millis/1000); alert(responseData.msg +'Duration(seconds): ' + duration); }, error: function (responseData, textStatus, errorThrown) { alert('POST failed. ' + JSON.stringify(responseData) + " status: " + textStatus + " Error: " + errorThrown); } }); } var products = [ { name: 'War Room Table', year: '2005', color: 'tan', country: 'USA', description: 'A Beautiful War Room table. Includes all 4 legs!', usaDollarPrice: 150 }, { name: 'Power Strip', year: '2000', color: 'white', country: 'USA', description: 'A very old power strip, may or may not protect against power surges.', usaDollarPrice: 16 }]; 

我的Node.js服务器端:

 exports.create = function(req, res) { var data = req.body; res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Content-Type', 'application/json'); //var parsed = JSON.parse(data); var msg = {}; for(product in data){ db.insert(data[product], function (err, body, headers) { if (!err) { msg.msg = 'success!'; console.log(JSON.stringify(msg); } else { msg.msg = 'Error on insert, maybe the item already exists: ' + err console.log(JSON.stringify(msg); } }); 

}}

我尝试了许多不同的东西。 我基本上想要的是服务器端的一个对象,我可以迭代并插入到云数据库,每个作为一个单独的文档。 我已经尝试了JSON.stringify和JSON.parse的许多组合。 使用parsing已经没有运气,因为我得到一个错误说SyntaxError:意外的令牌o我读,这意味着它已经是一个对象,但我不能以任何方式访问数据(data [0],data.name,data [0] .name,在服务器端没有)。 我也尝试以不同的方式从客户端发送JSON(在Ajax – 数据:JSON.stringify({产品:产品}),仍然没有运气。

在服务器端(与上面的产品一样)具有json以正确的顺序插入文档,问题是当我发送同一个json通过ajax文章和跨域服务器。 我无法得到这个JSON。 任何想法或帮助将非常感激,谢谢

解:

我结束了把对象到另一个数组,并使用它被发送到服务器。 这里是在客户端工作的Ajax,注意数据:{data:products}这是什么对我来说。 下面还有json的产品,以及如何在nodejs服务器端访问它。

 $.ajax({ type: 'POST', url: 'url/', crossDomain: true, data: {data:products}, dataType: 'json', contentType: "application/x-www-form-urlencoded", success: function(responseData, textStatus, jqXHR) { var end = new Date().getTime(); var millis = (end - start); var duration = (millis/1000); alert(responseData.msg +'Duration(seconds): ' + duration); }, error: function (responseData, textStatus, errorThrown) { alert('POST failed. ' + JSON.stringify(responseData) + " status: " + textStatus + " Error: " + errorThrown); } }); var products = [ { name: 'War Room Table', year: '2005', color: 'tan', country: 'USA', description: 'A Beautiful War Room table. Includes all 4 legs!', usaDollarPrice: 150 }, { name: 'Power Strip', year: '2000', color: 'white', country: 'USA', description: 'A very old power strip, may or may not protect against power surges.', usaDollarPrice: 16 }]; 

这里是如何在服务器端访问它。 请记住,这是所有跨域邮政,但应该工作,否则。

 exports.create = function(req, res) { var body = req.body; //body.data[0] will get you the first object in the json, in this case it will be the war room table. //If you use console.log to see it make sure you JSON.stringify(body.data[0]) or else you wil see [Object] [Object] 

在你的服务器/ app.js中包含这一点也是非常重要的扩展:真正的部分是重要的。 例

 app.use(bodyParser.urlencoded({ extended: true })); 

您将json数据发送到节点服务器之前进行string化,只尝试data:products您还需要使用它的节点模块body-parser ,您将能够通过ajax访问POST数据,也请参阅此答案

我是新来的阿贾克斯。 请检查是否有帮助。 我想我有一个类似的问题,现在已经解决了。

请看我贴的答案

如何发送数组数组作为$ .ajax()中的数据?