在相同的URL上,我 POST 来访问数据库的select选项的值,然后尝试通过 GET创建表 值
我检查过'hello'是通过console.log传递的,我也通过$.ajax({ success: function(data) { console.log(data) } })
进行了检查,但我不明白为什么它不呈现。
使用以下代码执行时,将成功警告“成功”。
/routes/route.js
const express = require( 'express' );
const router = express.Router();
const managepage = require( '../lib/managepage' );
router.use('/',(res,req) => {
managepage.Home( req,res)
})
/lib/managepage.js
var db = require ( './database/db' );
exports.Home = function ( req,res ) {
var hello = req.body.test;
var test = 3;
// I want to put POST data in this part.
db.manage_page(test).then((data) => {
var manage_db = data.testpagerow;
var manage_id = data.testpageid
res.render ('managepage',{ hello });
// I'll do it with {manage_db,manage_id},but I
// used a hello to test the POST data.
}).catch((errMsg) => {
console.log(errMsg);
});
}
managepage.ejs
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/static/css/index.css">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Manage Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<select name="test" id="ok">
<option>VALUE</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<h1><%=hello%></h1> //The problem with this is that it doesn't render.
<script>
$('#ok').change(function(e){
e.preventDefault();
$.ajax({
url: '',async: true,type: 'POST',data: {
test:$('#ok').val()
},dataType: 'text',success: function(data) {
alert('success')
},error: function(request,status,error) {
alert("code:"+request.status+"\n"+"error:"+error);
}
})
})
</script>
</body>
</html>
当我运行服务器并检查网络时,我看到表单数据已过帐,但是..不在ejs文件中呈现。
如何将POST数据呈现到ejs文件? 任何信息对我来说都是非常有价值的。