简体   繁体   English

使用 res.render get 后如何将数据发布到相同的 ejs 视图或部分?

[英]How to post data to the same ejs view or partial after using res.render get?

I need to display data that gets posted after rendering view with res.render() in get.我需要在 get 中使用 res.render() 显示渲染视图后发布的数据。 When I use <% date %> my view throws an error saying that date is not defined and I know this happens because my value does not exist before rendering the view and its getting set after.当我使用 <% date %> 时,我的视图会抛出一个错误,说日期未定义,我知道这是因为在呈现视图之前我的值不存在,并且在之后设置它。 So how do I set this variable to be displayed in my ejs file before or after my view gets rendered?那么如何设置这个变量我的视图被渲染之前或之后显示在我的 ejs 文件中呢?

I have tried a few things like using res.send(), using another get() inside my post() and even creating a partial view but nothing works.我尝试了一些方法,比如使用 res.send(),在我的 post() 中使用另一个 get(),甚至创建了一个局部视图,但没有任何效果。 I dont want to click any buttons to be able to do this and if its only possible with clicking well then.我不想点击任何按钮来做到这一点,如果只有点击好才有可能。 I know you cant use res.send() on the same view twice .我知道你不能在同一个视图上使用 res.send() 两次

My post XMLHttpRequest is executed when the page loads in javascript to set my date variable and send it to my node.js file then I use express to fetch it.当页面在 javascript 中加载以设置我的日期变量并将其发送到我的 node.js 文件时,我的帖子 XMLHttpRequest 被执行,然后我使用 express 来获取它。 My node.js code is the following:我的 node.js 代码如下:

app.get('/', function (req, res) {

    res.render('index', {
        time: time,
        views: visitors,
        mobileViews: mobileVisitors,
    });
}

app.post('/', function(req, res){
    console.log('working');

    getInfo = req.body;

    console.log(getInfo);

    //getInfo.date = 1

    res.render('index', {dataTest: getInfo.date}); //this doesnt work

    //OR res.send({dataTest: getInfo.date}); doesnt work

    //OR app.get('/', function (req, res) {res.render ... doesnt work
}

My ejs view is:我的 ejs 观点是:

<h1 class="test">test <%= dataTest %></h1>

My js file:我的js文件:

var waitForEl = function (selector, callback) {
    if (jQuery(selector).length) {
        callback();
    } else {
        setTimeout(function () {
            waitForEl(selector, callback);
        }, 100);
    }
};

waitForEl(".daterangepicker", function () {

    var element = document.querySelector('.ranges ul').childNodes[0].childNodes[0].data.toString();

    console.log(element);

    var date = 0;
    date = sendData(date, element);

    var obj = JSON.stringify({date: date});

    console.log(obj);

    var xhr = new XMLHttpRequest();

    xhr.open("POST", "http://localhost:4000", true);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

    xhr.send(obj);
});

function sendData(date, element) {


    if(element == 'Today') {

        date = 1;
    }

    return date;
}

try this :尝试这个 :

const bodyParser =require('body-parser');
const urlencodedParser = bodyParser.urlencoded({ extended: false });
app.get('/', function (req, res) {

    res.render('index', {
        time: time,
        Views: visitors,
        mobileViews: mobileVisitors,
    });
}

app.post('/',urlencodedParser,function(req, res){
    console.log('working');

    getInfo = req.body;

    console.log(getInfo);

    res.render('index', {getInfo: getInfo});

}

you need also a midlleware to handle post method in express app您还需要一个中间件来处理快递应用程序中的 post 方法

npm i body-parser

it's better to use the object's key name as its value name to ease cycling through the properties最好使用对象的键名作为它的值名以简化属性的循环

then in your ejs view use :然后在您的 ejs 视图中使用:

<%= getInfo.date %>

there is an = sign,don't forget it.有一个=符号,不要忘记它。

What I see is the first time you are trying to get "/" you don't have a getInfo.date我看到的是你第一次尝试获取 "/" 你没有getInfo.date

So make a getInfo.date = Date.now()所以做一个getInfo.date = Date.now()

Or getInfo.date = new Date()getInfo.date = new Date()

And pass it in into your initial get('/') call.并将其传递到您的初始 get('/') 调用中。

const bodyParser =require('body-parser'); const urlencodedParser = bodyParser.urlencoded({ extended: false }); app.get('/', function (req, res) { res.render('index', { time: time, Views: visitors, mobileViews: mobileVisitors, getinfo: getInfo}); } app.post('/',urlencodedParser,function(req, res){ console.log('working'); getInfo = req.body; console.log(getInfo); res.render('index', {getInfo: getInfo}); } const bodyParser =require('body-parser'); const urlencodedParser = bodyParser.urlencoded({ extended: false }); app.get('/', function (req, res) { res.render('index', { time: time, Views: visitors, mobileViews: mobileVisitors, getinfo: getInfo}); } app.post('/',urlencodedParser,function(req, res){ console.log('working'); getInfo = req.body; console.log(getInfo); res.render('index', {getInfo: getInfo}); } . const bodyParser =require('body-parser'); const urlencodedParser = bodyParser.urlencoded({ extended: false }); app.get('/', function (req, res) { res.render('index', { time: time, Views: visitors, mobileViews: mobileVisitors, getinfo: getInfo}); } app.post('/',urlencodedParser,function(req, res){ console.log('working'); getInfo = req.body; console.log(getInfo); res.render('index', {getInfo: getInfo}); } .

on the other hand you may also choose to make the getInfo.date = null or getInfo.date = ''另一方面,您也可以选择使getInfo.date = nullgetInfo.date = ''

And check in your ejs with if condition if(getinfo.date){<=getinfo.date>}并使用 if if(getinfo.date){<=getinfo.date>}条件检查您的 ejs

Trying from mobile.用手机试试。 Hope this solves希望这能解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM