post出错问题
发布于 9 年前 作者 wksmile 4534 次浏览 最后一次编辑是 8 年前 来自 问答

下面的代码用get方法在网页中能显示,并且提交url会改变,但是改为post后在网页主页中点击链接打开subform网页就报404错误。 //form.ejs <!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel=‘stylesheet’ href=’/stylesheets/style.css’ /> </head> <body>
<a href="/">首页</a> <br /> <a href="/subform">如何提交表单并接收参数?</a> <br /> <a href="/usesession">如何使用session?</a><br /> <a href="/usecookies">如何使用cookies?</a><br /> <a href="/usecrypto">如何字符串加密?</a> <form method=“post”> 用户名:<input type=“text” id=“txtUserName” name=“txtUserName” /> 密码:<input type=“password” id=“txtUserPwd” name=“txtUserPwd” /> <input type=“submit” value=“提交”> </form>
</body> </html> /********/ //form.js var express = require(‘express’); var router = express.Router(); router.post(’/’, function(req, res) { var userName = req.body.txtUserName, userPwd = req.body.txtUserPwd, userName2 = req.param(‘txtUserName’), userPwd2 = req.param(‘txtUserPwd’); console.log(‘req.body用户名:’+userName); console.log(‘req.body密码:’+userPwd); console.log(‘req.param用户名:’+userName2); console.log(‘req.param密码:’+userPwd2); res.render(‘subform’, { title: ‘提交表单及接收参数示例’ }); }); module.exports = router; // 主页点击链接就出错,其他的连接都有用: Not Found

404

Error: Not Found at D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\app.js:38:15 at Layer.handle [as handle_request] (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\layer.js:82:5) at trim_prefix (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:302:13) at D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:270:7 at Function.proto.process_params (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:321:12) at next (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:261:10) at D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:603:15 at next (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:246:14) at Function.proto.handle (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:166:3) at router (D:\Program Files\HBuilder\xiangmu\helloworld\nodejs-demo-express4\node_modules\express\lib\router\index.js:35:12)

5 回复

你没写路径,他没找到 你是新手吧,这类问题挺基础,你百度就好了

@THCloud你好,我是刚学node.js。我再在路由中加入一个get方法就可以运行了,运行的是post方法,为什么会这样呢?get方法可以单独运行,但是用post方法也需要把get方法同样的路径写到路由中

var name = req.body[‘name’], address = req.body[‘address’], tel = req.body[‘tel’], content = req.body[‘content’]; param是获取链接名的

app.get(‘remove/:name/:minute’ var name=req.params.name

@wksmile 那我推荐你先看一本计算机网络,然后明白各请求的意义是什么,然后你就明白为什么不行了,要不然你这样没有基础的话开发会很难

回到顶部