11

I was trying to make AJAX post request with JQuery to my Node.JS server in Node.JS and Express, but it doesn't works!

var express = require('express')
var app = express();
app.listen(8888);

app.configure(function(){

   app.use(express.bodyParser());
   app.set('views',__dirname + '/views');
   app.set('view engine', 'ejs');
   app.use(express.static(__dirname + '/public'));
   app.use(express.cookieParser());
   app.use(app.router);

});

app.get('/', function (req, res){

   res.render('ajax.ejs');

});

app.post('/ajax', express.bodyParser(), function (req, res){

   console.log(req);
   console.log('req received');
   res.redirect('/');

});

And this is the ajax.ejs:

<html>
<head>
  <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>    
  <script type="text/javascript">

      $('#enter').click(function(){  

     $.ajax({ 
           url: '/ajax',
           type: 'POST',
           cache: false, 
           data: { field1: 1, field2: 2 }, 
           success: function(data){
              alert('Success!')
           }
           , error: function(jqXHR, textStatus, err){
               alert('text status '+textStatus+', err '+err)
           }
        })
     });            

</script>
</head>
<body>

<form>
   <input type="button" id="enter" value="Enter">
</form>

</body> 
</html>

When the ajax.ejsis loaded, there isn't any output in the console, so, the post request didn't work. What can I do?

Thanks advance!

2
  • move express.bodyParser() to the app.use() section: app.use(express.bodyParser()); The body of the post you can retrieve via req.body. Commented Dec 27, 2012 at 21:48
  • Done! So, I change console.log(req); to console.log(req.body);. But the post request stills no working, I don't have any output in the console. Commented Dec 27, 2012 at 21:56

2 Answers 2

7

I found your problem. You need to move your script from your head to your body (after the form tag):

...
</form>
<script type="text/javascript">

   $('#enter').click(function(){  
   ...

</script>
</body>
Sign up to request clarification or add additional context in comments.

Comments

6

The below code works as per the new releases.

server.js

var express = require('express')
var app = express();
var bodyparser = require('body-parser');
var urlencodedparser = bodyparser.urlencoded({extended:false})

app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());

app.get('/', function (req, res){
   res.render('ajax.ejs');
});

app.post('/ajax', urlencodedparser, function (req, res){  
   console.log(req);
   console.log('req received');
   res.redirect('/');

});

app.listen(8888);

ajax.ejs

<html>
<head>
  <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>    

</head>
<body>

<form>
   <input type="button" id="enter" value="Enter">
     <script type="text/javascript">

      $('#enter').click(function(){  

     $.ajax({ 
           url: '/ajax',
           type: 'POST',
           cache: false, 
           data: { field1: 1, field2: 2 }, 
           success: function(data){
              alert('Success!')
           }
           , error: function(jqXHR, textStatus, err){
               alert('text status '+textStatus+', err '+err)
           }
        })
     });            

</script>
</form>

</body> 
</html>

Comments

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.