简体   繁体   English

使用Node.js将信息发送到服务器并获取处理后的数据

[英]Sending information to server and getting processed data back using nodejs

I'm trying to make an application using nodejs, express and ejs. 我正在尝试使用nodejs,express和ejs制作应用程序。 What I'm aiming to do is submitting a form to the server, trigger a server function which processes the data to get some result, and getting my page to display the result without reloading the whole page. 我的目的是向服务器提交表单,触发服务器功能来处理数据以获得一些结果,并让我的页面显示结果而无需重新加载整个页面。

Currently this is what I have: 目前,这是我所拥有的:

ejs EJS

<form method = "GET" action = "/sendinput"
...something here
</form>

app.js app.js

app.get('/sendinput', function (req, res) {
     result = processData(req.query);
});

I manage to get the function to run when the form is submitted, but after that the page keeps loading even after the function finishes running. 提交表单后,我设法使函数运行,但是此后,即使函数完成运行,页面仍会继续加载。 I'm also not sure how to fetch the result and let the form segment of the html displays it without reloading the whole page. 我也不确定如何获取结果并让html的表单段显示它而不重新加载整个页面。

To stop the page from loading, use res.end(); 要停止加载页面,请使用res.end();。 it will signal the server that your function is done. 它会向服务器发出信号,说明您的功能已完成。

app.get('/sendinput', function (req, res) {
   result = processData(req.query);
   res.end();
});

因此,最后我使用Ajax解决了该问题,该问题不会提示页面重新加载并设法获得我想要的内容。

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

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