1

First thank you for trying to help me. I want to know how pass(post,get) an Array in JS to Node.js backend to parse it then. This is my Array:

var Coordinates = [
      {lat: 37.772, lng: -122.214},
      {lat: 21.291, lng: -157.821},
      {lat: -18.142, lng: 178.431},
      {lat: -27.467, lng: 153.027}
                              ];

In the real stage, this Array is created when the user click the Google map. But this Array is an example what i need to send to my Node.js backend. Thank you

1 Answer 1

2

You can do JSON.stringify(). This will convert your array into a string, which can be passed across the network via GET or POST. In the node server, you can parse this string and get the array using JSON.parse. Adding a working Example below

var Coordinates = [
      {lat: 37.772, lng: -122.214},
      {lat: 21.291, lng: -157.821},
      {lat: -18.142, lng: 178.431},
      {lat: -27.467, lng: 153.027}
                              ];

var a = JSON.stringify(Coordinates)
console.log('Coordinates string: ', a)

var parsed = JSON.parse(a)
console.log('Parsed Coordinates: ', parsed)

Sign up to request clarification or add additional context in comments.

2 Comments

How can i Parse with node.js, i have this code in my backend, but it does not work: var jsonParser = bodyParser.json() app.post(':8081/load_coordinates',jsonParser, function(request, response){ var parsed = JSON.parse(request.body) console.log(parsed); }); app.listen(8081); In my frontend i have this: function httpPOST(data) { var client = new XMLHttpRequest(); var url = "localhost:8081/load_coordinates"; client.open("POST", url, true); client.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); client.send(data); }
You can do JSON.parse() in node js too.

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.