4

i am sending form data to node.js server using ajax function in html page if i use post method i am not able see anything in console as well as alert box.

if i use get method in ajax and app.js file i am getting alert as hello but i am not any msg in console of app.js server

app.js

var express = require('express')
, routes = require('./routes')
, manage = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();
app.post('/filter', routes.filter);
http.createServer(app).listen(app.get('port'), "172.29.59.65", function(){
console.log('Express server listening on port ' + app.get('port'));
});

index.js

exports.filter = function(req, res){

req.on('data', function (chunk) {  
console.log("Received body data", chunk.toString());
    //chunk is received, process it as you need
  })
req.on('end', function() {
    // http request is competed, send response to client
    res.writeHead(200, "OK", {'Content-Type': 'text/html'});
    res.end("hello");
  })    
};

client.html

<html>
<head>
<script>
function myFunction() { 

var region=document.getElementById("region").value;
var os=document.getElementById("os").value;
alert(region+os);
var data = {};
 data.region = region;
 data.os = os;
 $.ajax({
 type: 'POST',
 jsonpCallback: "callback",
 datatype: 'jsonp',
 data: JSON.stringify(data),
 //contentType: 'application/json',
 url: 'http://172.29.59.65:3000/filter',
 success: function(data) {
   alert(data);
   console.log(JSON.stringify(data));
 },
 error: function (xhr, status, error){
    console.log('Failure');
    alert("failure");
    },
}); 
}
</script>
</head>
<body>
<form>
<input type="text" id="region" name="region"/>
<input type="text" id="os" name="os"/>
<input type="button" value="search" class="fil_search" onclick="myFunction()"/>
</form>
</body>
</html>

my problem is send data to node server and receive the data from node server to html page how to achieve this. please help me out of this.

1 Answer 1

1

You need a middleware to parse post data of the body. You can try bodyParser. First, you should install it:

$ npm install body-parser

And then just require and use it:

var express = require('express')
, bodyParser = require('body-parser')
, routes = require('./routes')
, manage = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/filter', routes.filter);
http.createServer(app).listen(app.get('port'), "172.29.59.65", function(){
console.log('Express server listening on port ' + app.get('port'));
});

Now your POST parameters will be stored in req.body.

exports.filter = function(req, res){
    console.log("Received body data", req.body);
    res.writeHead(200, "OK", {'Content-Type': 'text/html'});
    res.end("hello");
 })    
};
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this but not working again "failure" message and along with error called "uncaught exception: out of memory"
One more moment I've missed, use req.body instead of chunks, I will update my answer in a minute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.