【问题标题】:JQuery Ajax post request with node.js and express使用 node.js 和 express 的 JQuery Ajax 发布请求
【发布时间】:2012-12-13 06:06:32
【问题描述】:

我试图在 Node.JS 和 Express 中使用 JQuery 向我的 Node.JS 服务器发出 AJAX 发布请求,但它不起作用!

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('/');

});

这是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>

ajax.ejs被加载时,控制台没有任何输出,所以post请求没有工作。我能做什么?

提前感谢!

【问题讨论】:

  • 将 express.bodyParser() 移动到 app.use() 部分:app.use(express.bodyParser());您可以通过 req.body 检索的帖子正文。
  • 完成!所以,我将console.log(req); 更改为console.log(req.body);。但是发布请求仍然无效,我在控制台中没有任何输出。

标签: jquery ajax node.js post express


【解决方案1】:

我发现了你的问题。您需要将脚本从头部移动到身体(在表单标签之后):

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

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

</script>
</body>

【讨论】:

    【解决方案2】:

    以下代码根据新版本运行。

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-23
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多