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:
var fs = require('fs'),
express = require('express'),
app = express();
app.listen(8080);
app.use(express.bodyParser());
app.get('/', function(req, res){
var html = fs.readFileSync('index.html');
res.header("Content-Type", "text/html");
res.send(html);
});
app.post('/deleteIds', function(req, res){
console.log(req.body.arr[0]);
res.json({ success: true });
});
The server '/' request returns an HTML page that will create an jQuery Ajax request, Here is the HTML file content:
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
url: 'http://localhost:8080/deleteIds',
type: 'POST',
data: {
arr: [ 1, 2, 3]
},
success: function(data){
console.log(data);
}
});
</script>
</head>
<body>
</body>
</html>
As you can see, the Ajax request in the html file sent as a POST request with an array as the data, The /deleteIds
express path POST function in the server listens to that and takes the array using req.body.arr
, note that I'm using app.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 arr
and do what you want with the data.