2016-06-28 88 views
0

我想使用節點/快遞渲染一些文本。 我有一個html文件包含一個表單。node/express - 如何呈現用戶輸入到頁面?

search.ejs

$(document).ready(function(){ 
    var userInput; 
    $("#submit-button-id").click(function(){ 
     userInput = $1_11_1("#userInput").val(); 
     $.get("http://localhost:3000/searching", { 
      userInput: userInput 
     }, function(){}); 
    }); 
}); 

app.js

app.get('/searching', function(req, res){ 
    var userInput = req.query.userInput; 

    /** I am able to get the userInput (the code above works) */ 

    res.send(userInput); 

    /** I want to render the userInput (for instance, on localhost:3000/results) */ 
}); 

任何幫助/鏈接將不勝感激,謝謝!

回答

1

在你app.js,您發送的userInput信息返回到搜索。 EJS。這是位置:

$(document).ready(function(){ 
    var userInput; 
    $("#submit-button-id").click(function(){ 
     userInput = $1_11_1("#userInput").val(); 
     $.get("http://localhost:3000/searching", { 
      userInput: userInput 
     }, function(){***User data can be used here***}); 
    }); 
}); 

所以下面的代碼將努力使您的原始數據轉換成HTML網頁上一個div稱爲渲染格:

$(document).ready(function(){ 
    var userInput; 
    $("#submit-button-id").click(function(){ 
     userInput = $1_11_1("#userInput").val(); 
     $.get("http://localhost:3000/searching", { 
      userInput: userInput 
     }, function(userInputData){ 
      $('.render-div).append(userInputData); 

     }); 
    }); 
});` 
0

按照documentation你有響應,在回調參數,所以你應該嘗試這樣的事:

$.get("http://localhost:3000/searching", {userInput: userInput}, 
    function(response){ 
     alert(response); 
}); 
0

如果您使用EJS可以像這樣做:

首先,在你results.ejs文件

<div id="displayResult"><h2><%= result %></h2></div> 

然後,在你app.js

app.get('/searching', function(req, res){ 
var userInput = req.query.userInput; 
res.render('results.ejs',{result:userInput}); 
});