简体   繁体   English

Express.js - 如何使用 res.render 从 Express 向客户端发送警报? 不重新发送

[英]Express.js - How to send alert from Express to Client using res.render? Not res.send

I want to send the alert message to client.我想向客户端发送警报消息。

So, I found that.所以,我发现了。

res.send(<'alert("XXX!!!")'); res.send(<'alert("XXX!!!")');

In this way, I didn't use res.render();这样,我就没有使用 res.render();

I want to use together.我想一起使用。

Or或者

I want to send alert message using res.render();我想使用 res.render() 发送警报消息;

How do I this?我这个怎么办

This is the code.这是代码。

 if (err) console.log(err); if (flag === 1) { //함수 이름 중복 try { //res.send('<script>alert("함수 이름 중복!")</script>'); LoadDB(res); } catch (exception) {} } else { //(4) 데이터베이스 insert 쿼리 var query = "insert into automation_script set ?"; conn.query(query, set, function (error, result, fields) { if (error) { console.log('Insert_DB error' + error); } else { try { LoadDB(res); } catch (exception) {} } }); } //(5) 데이터베이스 해제 conn.end();

Thanks you.谢谢。

There are a lot of problems here.这里有很多问题。 For one I'm not sure you understand how try catch blocks work.一方面,我不确定您是否了解 try catch 块的工作原理。 You have an unnecessary amount of ifs that aren't being handled too.您也有一些不必要的 if 没有被处理。 Those problems aside, the answer to the question you asked is a websocket.撇开这些问题不谈,您提出的问题的答案是 websocket。 From the looks of your code and question, I think what you're really looking for is how to send a response from an http get request in express.从您的代码和问题的外观来看,我认为您真正要寻找的是如何从 http get 请求中以 express 发送响应。 There are many ways to solve the problem you are asking to be solved.有很多方法可以解决您要求解决的问题。 From the code I see above I don't think you have a good understanding of client/server models and It would be to your benefit to read up on how http works.从我上面看到的代码中,我认为您对客户端/服务器模型没有很好的理解,阅读 http 的工作原理对您有好处。 I also can't answer your question because the code snippet you pasted does not provide enough context to answer your question.我也无法回答您的问题,因为您粘贴的代码片段没有提供足够的上下文来回答您的问题。 Most importantly though, alert is not a function in node.但最重要的是,警报不是节点中的功能。 calling alert does nothing.调用 alert 什么都不做。 In fact when you do res(alert("something")), if alert is defined in your global namespace (I hope it isn't), you are sending back undefined.实际上,当您执行 res(alert("something")) 时,如果在您的全局命名空间中定义了警报(我希望不是),那么您将发送回未定义的。 There is such a thing as isomorphism, however, web api functions are exclusive to the client.有同构之类的东西,但是,web api 函数是客户端独有的。 I hope that helps.我希望这有帮助。

res.send( <script>alert("your alert message"); window.location.href = "/page_location"; </script> );

res.send("alert("your alert message"); window.location.href = "/page_location"; "); res.send("alert("你的提醒信息"); window.location.href = "/page_location"; ");

same answer as M.HUSSAIN only he forgot the string quotes aroud the expression与 M.HUSSAIN 相同的答案只是他忘记了表达式周围的字符串引号

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

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