Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

javascript - 在EJS模板中获取表单参数(Get form parameter in EJS template)

Hello I'm learning to use Nodejs on my own, and I'm struggling on something that looks really really simple but with fatigue I can't seem to make it work even with an embarassing number of google searches.(您好,我正在学习自己使用Node.js的方法,我正在努力尝试看起来非常简单的方法,但是由于疲劳,即使使用了大量的Google搜索,我似乎也无法使其正常工作。)

I tried to unstuck myself but I need help.(我试图解开自己,但我需要帮助。)

Here is part of my code.(这是我的代码的一部分。)

I use EJS for the template.(我使用EJS作为模板。)
var express = require('express');
var request = require('request');
var bodyParser = require("body-parser");
const querystring = require('querystring');
const url = require('url');


var app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
const fetch = require("node-fetch");


// URL par défaut
 app.get('/', function (req, res) {
    res.render('index');
 });
//------------------------------------------------------
// Client
 app.get('/client', function (req, res) {
    res.render('client');
 });
 // Edit Client
 app.get('/Eclient, function (req, res) {
    res.render('eclient', {query : req.query});
 });

// set port
 app.listen(3001, function () {
     console.log('Node app is running on port 3001');
 });
 module.exports = app;

On my Client page there is a form that is sending to Eclient a parameter called pIDC, with an URL like this : http://localhost:3001/Eclient?pIDC=1 I want to get the value of pIDC on my EJS page with something like <%= req.query.pIDC%>;(在“我的客户”页面上,有一种形式正在向Eclient发送一个名为pIDC的参数,该参数具有如下URL: http://localhost:3001/Eclient?pIDC=1我想通过以下方法在我的EJS页面上获取pIDC的值:类似于<%= req.query.pIDC%>;)

so I can in a second time query the data of the client from my API.(因此,我可以第二次从我的API查询客户端的数据。) But I can't seem to make it work, everytime the data is undefined.(但是,每次数据未定义时,我似乎都无法正常工作。)

I'm sure it's trivial, but I can't seem to make it work even with all the searches I did.(我敢肯定它是微不足道的,但是即使我进行了所有搜索,我也似乎无法使其正常工作。)

What am I missing ?(我想念什么?)   ask by Sareth translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's right, you pass to the template through the variable "query" like {query: req.query} and in template EJS you get the variable "query" and show it.(没错,您通过变量“ query”(如{query:req.query})传递到模板,然后在模板EJS中获得变量“ query”并显示它。)

I include "if" for don't show the variable "query" if we don't have query variable from url.(如果我们没有来自url的查询变量,则包括“ if”以便不显示变量“ query”。) You can learn more in documentation on EJS(您可以在有关EJS的文档中了解更多信息)
<% if (query && query.pIDC) { %>
<%= query.pIDC %>
<% } %>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...