2011-09-22 4 views
25

나는 express를 사용 중이며 bodyParser에서 양식 데이터를 가져 오는 데 문제가 있습니다. 내가 뭘 할지라도 항상 빈 객체로 나타납니다. 나는를 제출하면Node.js/Express form post req.body not working

<!doctype html> 
<html> 
    <body> 
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded"> 
    <input type="text" id="mytext" /> 
    <input type="submit" id="mysubmit" /> 
</form> 
    </body> 
</html> 

: 여기

var express = require('express'); 

var app = module.exports = express.createServer(); 

// Configuration 

app.configure(function(){ 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(__dirname + '/public')); 
}); 

app.configure('development', function(){ 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

app.configure('production', function(){ 
    app.use(express.errorHandler()); 
}); 

// Routes 

app.get('/', function(req, res){ 
    res.sendfile('./public/index.html'); 
}); 

app.post('/', function(req, res){ 
    console.log(req.body); 
    res.sendfile('./public/index.html'); 
}); 

app.listen(3010); 

내 HTML 양식입니다 : 여기 내 표현이 app.js 코드를 생성이다 (I 추가 한 유일한 것은 하단에있는 app.post 노선이었다) 형태 req.body은 빈 개체 {}이 내가 양식 태그

에서에 enctype 속성을 제거 할 경우에도 발생하는 것을주의 그것의 가치가

... 내가 놓친 거지 뭔가 거기에가/잘못하고있다?

내가 노드 v0.4.11 및 표현 v2.4.6

답변

33

HTTP를 포스트의 본문을 사용하고하는 것은 name 속성을 가진 모든 폼 컨트롤의 키/값 해시이며, 값은이다 컨트롤의 값.

모든 입력에 이름을 입력해야합니다.

+0

고맙습니다! 어떻게 내가 그것을 간과하는지에 관해 명확히하지 않는다. .. – binarymax

4

콘텐츠 유형에도 영향을줍니다. console.log (req) 객체를 참조하십시오.

'content-type': 'application/json; charset=UTF-8’ // valid. 

'content-type': 'application/JSON; charset=UTF-8’ // invalid & req.body would empty object {}. 

내가 생각

true/false를 반환 // CONSOLE.LOG에 의한 콘텐츠 유형 (req.is ('JSON')를) 확인하려면 '문자셋 = UTF-8'이상에서 무시할 수있다.