RT: app.post('/views/validation.html’,function(req,res){ console.log(req.body); });
post 到这个页面的时候, 答应出undefiend; 客户端的form 代码如下:
<form id="text_form" method="POST" action="/views/validation.html"> name:<input type="text" id="name"></ br> age:<input type="text" id="age"></ br> <input class="submit" type="submit" value="提交"/> </form> 求 怎么获取post提交的数据@MiguelValentine 我看其他人的帖子都是用req.body 这样来获取Post过来的值,用过req.parmes 或者rqp.query来获取get过来的值,我自己测试过get过来的值 是可以获取到的,但是post过来的就是找不到
//app.js var routes = require(‘…/route’); …… 在app.use(express.static(path.join(__dirname, ‘…/public’)));app.use(cookieParser(‘*’));之后插入 app.use(router) // route/index.js var express = require(‘express’); var router = express.Router(); router.post('/views/validation.html’,function(req,res){ console.log(req.body); });
@MiguelValentine 这是我server.js的全部代码 var express = require(‘express’); var ejs = require(‘ejs’); var sio = require(‘socket.io’); var app = express();
var urlrouter = require(‘urlrouter’); app.set(‘view engine’,’html’); app.set('views’,__dirname+"/views"); app.set('view option’,{layout:false}); app.engine(‘.html’, ejs.__express); app.use(express.static(__dirname+’/public’));
app.post('/’,function(req,res,next){ if(req.query.name!=null) { //res.send(req.query.name); /res.json([{"name":req.query.name,"id":1,"age":18},{"name":"张三","id":2,"age":12}, {"name":"Tmo","id":3,"age":13}, {"name":"Jerry","id":4,"age":14}, {"name":"Jim","id":5,"age":15}]);/ console.log(req.query.name); } else { res.send(‘err’); console.log(‘err’); } });
app.get('/',function(req,res,next){
res.render('index');
});
app.get('/views/*.html',function(req,res,next){
res.render(req.params[0]);
//console.log(req.query.id);
});
app.post('/views/validation.html',function(req,res,next){
console.log(req.query);
console.log(req.body);
});
/*var router = urlrouter(function (app) {
});*/
app.listen(3000);
里面app.get或者app.post 对应的代码块都是会执行的,唯一的问题就是post方法里面取值 取不到,刚才我根据别人的例子,把所有的请求代码放到 /*var router = urlrouter(function (app) {
});*/代码块中去,结果就是连首页都找不到了
var express = require(‘express’); var ejs = require(‘ejs’); var sio = require(‘socket.io’); var app = express();
var urlrouter = express.Router(); app.set(‘view engine’,’html’); app.set('views’,dirname+"/views"); app.set('view option’,{layout:false}); app.engine(‘.html’, ejs.express); app.use(express.static(__dirname+’/public’)); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json());
urlrouter.post('/’,function(req,res){ if(req.query.name!=null) { console.log(req.query.name); } else { res.send(‘err’); console.log(‘err’); } });
urlrouter.get('/’,function(req,res){ res.render(‘index’); }); urlrouter.get('/views/*.html’,function(req,res){ res.render(req.params[0]); //console.log(req.query.id); });
urlrouter.post('/views/validation.html’,function(req,res){ console.log(req.body); });
@MiguelValentine 这是按你说的,整理以后的代码
var express = require(‘express’);
var ejs = require(‘ejs’);
var sio = require(‘socket.io’);
var app = express();
var urlrouter = express.Router();
app.set(‘view engine’,’html’);
app.set('views’,__dirname+"/views");
app.set('view option’,{layout:false});
app.engine(‘.html’, ejs.__express);
app.use(express.static(__dirname+’/public’));
urlrouter.post('/’,function(req,res){
if(req.query.name!=null)
{
console.log(req.query.name);
}
else
{
res.send(‘err’);
console.log(‘err’);
}
});
urlrouter.get('/’,function(req,res){
res.render(‘index’);
});
urlrouter.get('/views/*.html’,function(req,res){
res.render(req.params[0]);
});
urlrouter.post('/views/validation.html’,function(req,res){
console.log(req.body);
});
app.listen(3000);
这样写的话 我chmore http://localhost:3000/ 页面显示的就是Cannot GET /
@ciiii 把你的那两句代码加进去以后 报错说是 bodyParser未定义。我刚才也翻了一下书,说bodyParser是conncet中间件的一个插件,然后我也安装了一下conncet 然后再用bodyParser 说是connect.bodyParser 方法未定义
@MiguelValentine ReferenceError: bodyParser is not defined at Object.<anonymous> (C:\Users\hc\Desktop\node\server.js:12:9) at Module._compile (module.js:456:26) at Object.Module._extensions…js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3
@ciiii { “name":"express-tweet” ,"version":"0.0.1" ,"dependencies":{ “express":"4.5.1” ,"ejs":"0.4.2" ,"superagent":"0.3.0" ,"connect":"3.0.2" }
}
@ciiii @MiguelValentine 用express直接调用bodyparser 会报错说是没有这个方法,然后我安装了一个body-parser的组件。 运行正常了,可是获取到的req.body 为{}.
app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use(express.static(path.join(__dirname, ‘…/public’))); 这个顺序。加进去。
@MiguelValentine 有个问题非常的奇怪,我贴出代码 你看下 这个是我html的页面 <html> <head>
<script language="javascript" type="text/javascript" src="/js/jquery.js"></script> <script language="javascript" type="text/javascript" src="/js/jquery.validate.min.js"></script> <link rel="stylesheet" href="css/ui.jqgrid.css"> <script type="text/javascript" > $(function(){ $.ajax({
type: 'POST',
url: '/views/validation.html',
data: { name: "John", age: 18 },
success: function(data){
alert("Data Loaded: " + data);
},
dataType: 'text'
});
});
function fun()
{
$.ajax({
type: 'POST',
url: '/views/validation.html',
data: { name: "John", age: 18,address:"aaa" },
success: function(data){
alert("Data Loaded: " + data);
},
dataType: 'text'
});
}
</script>
</head>
<body>
</body>
<form id="test_form" method="POST" action="/views/validation.html">
name:<input type="text" id="name"></ br>
age:<input type="text" id="age"></ br>
<button onclieck="fun()"> click</button>
<input class="submit" type="submit" value="提交"/>
</form>
</html>
用的Jquery 界面加载的时候就会执行一次$.ajax ,会发送一个{ name: "John", age: 18 }数据目标页面 也就是本页面,然后对应的请求方法是: app.post('/views/validation.html’,function(req,res,next){ console.log(“post”); console.log(req.body); }); 第一次会输出正确的数据。可是当我点击click按钮手动触发fun()函数的异步请求的时候,req.body显示为{}.诶,这个问题真心搞不懂了,如果可以的话我把代码发给你们,帮我看看吧
@MiguelValentine 真无奈了,为什么我的项目中都无法调用conncet下的中间件呢?刚才bodyparser,你刚才说的logger 都不行,然后我npm install logger,但是还是不行。把app.use(logger(‘dev’));注释掉以后,还是出现我刚才说的那样的问题,就页面第一次加载会有值,以后请求就没有