NodeJs Express App.

It is necessary to transfer an array from the client to the server.

On the client I form an array:

var path = []; for(var i = 0; i < someObject.lenght; i++) { path.push({ "path_x" : someObject[i].getAttribute("d")}); } 

I send to the server:

 $.ajax({ type: 'POST', url: '/getPaths', data: path, success: function (resp) { alert(resp); }, error: function (xhr, str) { alert('Возникла ошибка: ' + xhr.responseCode); } }); 

On the server, trying to get an answer: /route/index.js:

 var router = express.Router(); router.post('/getPaths',function(req, res, next){ console.log(req.body); res.end(); }); 

On the server I get a response, something like this:

 { 'undefined': '' } 

How to transfer an array to the server?

1 answer 1

https://ru.wikipedia.org/wiki/JSON#Syntax

since you are using JQuery do not guarantee, but try passing the variable qwa to the data object

 let arr = [1, 2, 3] let obj = { data: arr } let qwa = JSON.stringify(obj) // console.log(qwa)//{"data":[1,2,3]} // console.log(typeof qwa)//string $.ajax({ type: 'POST', url: '/getPaths', data: qwa, success: function (resp) { alert(resp); }, error: function (xhr, str) { alert('Возникла ошибка: ' + xhr.responseCode); } }); 

if it doesn't help, add a JQuery tag to the question, and yes, there is a high probability that this question has already been asked