Sunday, 14 May 2017

How to read the data from node js post ajax request?

I am trying to send a data from client to server (node js) . I am using ajax .

client :

$("#test_button").on("click",function(){
    //alert("shit");
    $.ajax(
        {url: "http://localhost:4000/ajax_check", 
        async: false, 
        type: "POST",
        data: "{user:balayya,password:hero}",
        success: function(result){
            alert("hooli");
        }});
});

server:

var app = require('express')();
var express = require('express');
var http = require('http').Server(app);


http.listen(process.env.PORT || 4000, function() {
    console.log('listening on *:4000');
});

app.use(express.static('publuc'));

app.get('/', function(req, res) {
    console.log("new entry page serving");
    res.sendFile(__dirname + '/main.html');
});

app.post('/ajax_check', function(req, res){
    console.log("someone came in here");
    console.log(req.query.data);
});

the console.log() is printing as "undefined" . What is the correct way to receive a post request and it's data from the client in node js



via BloomBlack

No comments:

Post a Comment