Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive data from an AJAX POST function at the server side with Node.js?

I am trying to send data to the server with the Ajax POST function, and then receive it at the server side with Node.js (and then manipulate it there) but the only problem is that I am unable to find any function at the Node.js side to allow me,accomplish this.I would really like it if you guys could help me out on how to do this as even related threads, I visited on many websites were not very helpful.

Thanks

like image 917
TheGhostJoker Avatar asked Sep 14 '25 20:09

TheGhostJoker


1 Answers

It will be much easier for you to use some Node-framework like express, to handle all that routes and requests.

You can install it and body-parser module with these commands:

npm install express --save
npm install body-parser --save

Visit express API References to find out more: http://expressjs.com/4x/api.html

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());

// Handle GET request to '/save'
app.get('/save', function(req, res, next){
  res.send('Some page with the form.');
});

// Handle POST request to '/save'
app.post('/save', function(req, res, next) {
  console.log(req.body);
  res.json({'status' : 'ok'});
});

app.listen(3000);

Inside your app.post() route you can get access to any post data using req.body. So your S_POST["name"] will be req.body.name in this case.

like image 112
Kevin Avatar answered Sep 17 '25 09:09

Kevin