I have array of ids like the follows:
var arr = [1, 23, 6];
I need to send this array to a server, parse it, and remove the ids from a mongoose db. For my backend I’m using nodejs express. So I need your advise how should I send the array by using jQuery and how should I parse it on a server.
Ok, follow this instructions and you are good:
first of all, lets create our node server, I’m using the Express module to define the HTTP server:
The server ‘/’ request returns an HTML page that will create an jQuery Ajax request, Here is the HTML file content:
As you can see, the Ajax request in the html file sent as a POST request with an array as the data, The
/deleteIdsexpress path POST function in the server listens to that and takes the array usingreq.body.arr, note that I’m usingapp.use(express.bodyParser());, without this line we will not be able to get the body content from the POST request.This is it.. I hope this is helping understand Node + Ajax requests using express, from this point you can run on the
arrand do what you want with the data.