Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





How to Get and Post Data using Ajax in node js?

View :-

File Name : index.html







Hello people !









Route controller

File Name : server.js

const express = require("express");
const bodyParser = require("body-parser");
const router = express.Router();
const app = express();
// add router in express app
app.use("/",router);

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

router.get(‘/’,(req, res) => {
res.sendfile(“index.html”);
});

router.post(‘/login’,(req, res) => {
var user_name = req.body.user;
var password = req.body.password;
console.log(“User name = “+user_name+”, password is “+password);
res.end(“yes”);
});

app.listen(3000,() => {
console.log("Started on PORT 3000");
})

Html View page

File Name :








File Name :

app.post("/getCity", (req, res) => {
var cityname= req.body.city;
var country= req.body.country;
city.findCity(cityname, country).then((cityID) => {
res.status(200).send({ cityID: '123' });
}).catch((e) => {
res.status(400).send(e);
});
});





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here