2013-03-12 36 views
0

我想知道的命令,通過POST方法從HTML發送的接入參數表格從的NodeJS/ExpressJS訪問參數通過POST方法從HTML形式發送

我已經嘗試了所有這些,但他們打印undefined或發回error ...

編輯1:忘了提,我也加入了這個server.use(express.bodyParser());

編輯2:

var express = require('express'), 
server = express(), 
client = require('mysql').createConnection({ host:'localhost', user:'root', password:'password' }); 

在MySQL

var dbcrud = require('dbcrud').init(client, 'contacts', model); 

然後使用dbCRUD休息服務我做了

server.use(express.bodyParser()); 

後的功能在這裏進行..

server.post('/families',function(req,res){ 
    console.log(req.id); 
    console.log("1: " + req.param.id); 
    console.log("2: " + req.params.id); 
    console.log("3: " + req.param("id")); 
    console.log("4: " + req.params('id')); 
    console.log("5: " + req.params[0]); 
    console.log("6: " + req.body.name); 
    console.log("7: " + req.body.notes); 
    console.log("8: " + req.body.id); 
    //console.log("5: " + req.params[0]); 
    res.send('Hello POST : families'); 
}); 

我添加路由這裏...

dbcrud.addRoutes(server); 

在HTML表單我指定的ID和輸入的標籤名稱屬性...

<form method="post" action="http://10.180.218.72:3000/families"> 
    id : <input type="number" id="id" name="id"></input> 
    name : <input type="text" id="name" name="name"></input> 
    notes : <input type="text" id="notes" name="notes"></input> 
      <input type="submit" value = "submit" ></input> 
</form> 
+0

你能發佈更多的代碼嗎?或者至少訂購中間件,路由和app.listen出現在你的代碼中? – soulcheck 2013-03-12 10:58:37

+0

編輯,請看看 – 2013-03-12 11:09:11

回答

0

嘗試app.use(express.bodyParser());在你的代碼。 bodyParser是解析post請求體的快速中間件。

+0

編輯的問題..我已經有了代碼 – 2013-03-12 10:46:59

+0

也添加enctype =「application/x-www-form-urlencoded」到您的表單html代碼 – MrPovod 2013-03-12 10:54:43

+0

補充說,不工作仍然.. – 2013-03-12 11:02:25