I am developing an app on node.js, express.js and mongodb. My task is to pass an array to server-side and insert it to db. I've searched throughout the web and found that I need to stringify my array via JSON.stringify(), but when I console it on server side it looks strange and every attempt to use JSON.parse() ends with error. Here is an array which is passed through JSON.stringify():
[{
"city": "London",
"date": "20.11.2015",
"sector": "A",
"row": "1",
"place": "1"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "A",
"row": "1",
"place": "2"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "A",
"row": "2",
"place": "1"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "A",
"row": "2",
"place": "2"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "B",
"row": "1",
"place": "1"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "B",
"row": "1",
"place": "2"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "B",
"row": "2",
"place": "1"
}, {
"city": "London",
"date": "20.11.2015",
"sector": "B",
"row": "2",
"place": "2"
}]
when I pass it to server side it looks like this:
{ '[{"city":"London","date":"20.11.2015","sector":"A","row":"1","place":"1"},{"city":"London","date":"20.11.2015","sector":"A","row":"1","place":"2"},{"city":"London","date":"20.11.2015","sector":"A","row":"2","place":"1"},{"city":"London","date":"20.11.2015","sector":"A","row":"2","place":"2"},{"city":"London","date":"20.11.2015","sector":"B","row":"1","place":"1"},{"city":"London","date":"20.11.2015","sector":"B","row":"1","place":"2"},{"city":"London","date":"20.11.2015","sector":"B","row":"2","place":"1"},{"city":"London","date":"20.11.2015","sector":"B","row":"2","place":"2"}]': ''}
and any attempt to parse it leads to 500 error. So what am I doing wrong?
$.ajax({ method: "POST", url: "/tickets/addticket", dataType: "json", data: JSON.stringify(dataToSend) });