0

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?

3
  • How are you passing it to the server? Commented Dec 29, 2015 at 22:56
  • @epascarello $.ajax({ method: "POST", url: "/tickets/addticket", dataType: "json", data: JSON.stringify(dataToSend) }); Commented Dec 29, 2015 at 23:00
  • How are you trying to parse it? Commented Dec 29, 2015 at 23:06

1 Answer 1

4

Do you see what it is doing? It is using the JSON you passed up as the key with no value.

You can just do

$.ajax({ 
   method: "POST", 
   url: "/tickets/addticket", 
   dataType: "json",
   data : { val : JSON.stringify(dataToSend) }
}

and the node.js read the querystring value for val

Other option is to set the content type

$.ajax({
    url: "/tickets/addticket", 
    type: 'POST', 
    contentType: 'application/json', 
    data: JSON.stringify(dataToSend)}
);
Sign up to request clarification or add additional context in comments.

1 Comment

First way is right too, just would need to change how you were getting the data, but the second way is normally how it is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.