简体   繁体   English

使用 jquery 从 express 的 post 方法中检索数据

[英]Retrieve data from a post method of express with jquery

I am using express.js for a dynamic website, and I decided to use jQuery to send the post method, so that I can create multiple buttons and send different information depending on the button pressed by the user on one and same page.我正在将 express.js 用于动态网站,我决定使用 jQuery 发送 post 方法,以便我可以创建多个按钮并根据用户在同一页面上按下的按钮发送不同的信息。 Here is my simplified environment:这是我的简化环境:

html file: html 文件:

<% if(alert) { %>
<div class="alert info"><i class="fas fa-info-circle"></i> Si tu viens de souscrire à un abonnement, patiente une à deux minutes.</div>
<% } %>

<form>
  <input id="premiumKey" placeholder="Enter your key here !"></input>
  <input id="save-button-key" class="sub save key" type="button" value="Save"></input>     
</form>   

<script type="text/javascript">
    $('#save-button-key').click(function(){  

    let key = document.getElementById("premiumKey").value
   $.ajax({ 
         url: '/profil/premium',
         type: 'POST',
         cache: false, 
         data: { premiumKey: key, input: 2 }, 
         success: function(data){

         }
         , error: function(jqXHR, textStatus, err){

         }
      })
   });            

</script>

express.js file: express.js 文件:

  const renderTemplate = async (res, req, template, data = {}) => {

    const baseData = {
      bot: client,
      path: req.path,
      user: req.isAuthenticated() ? req.user : null,
    };
    // We render template using the absolute path of the template and the merged default data with the additional data provided.
    res.render(path.resolve(`${templateDir}${path.sep}${template}`), Object.assign(baseData, data));
  };


  app.get("/profil/premium", async (req, res) => {
    
    renderTemplate(res, req, "profil/premium.ejs", { alert: null });
  });

  app.post("/profil/premium", async (req, res) => {
    console.log(req.body)
    console.log(req.body.premiumKey)

    var user = req.user;

    let alert = true;
    renderTemplate(res, req, "profil/premium.ejs", { alert });
  });

What i would like I would like to recover when I press the button, the alert that I get with the post method in the express file.按下按钮时,我想要恢复的内容是我使用 express 文件中的 post 方法获得的警报。 I can display what ajax sends to the server, but I can't do the reverse during a post method.我可以显示 ajax 发送到服务器的内容,但在 post 方法中我不能做相反的事情。 For example, I would like to analyze the information, and return the result to the user.例如,我想分析信息,并将结果返回给用户。 I can't do it so far.到目前为止我做不到。

Thank you again for your help.再次感谢你的帮助。

I suggest to you, send the response using json.我建议您使用 json 发送响应。 Instead of render a templete而不是渲染模板

renderTemplate(res, req, "profil/premium.ejs", { alert });

Send de data in json format以json格式发送de数据

const alertData = {
value: 1,
otherValue: "the alert data"
};
res.json(alertData);

Then in the front, in jquery, in the succes function you will recieve the data然后在前面,在 jquery 中,在成功 function 中,您将收到数据

$.ajax({ 
         url: '/profil/premium',
         type: 'POST',
         cache: false, 
         data: { premiumKey: key, input: 2 }, 
         success: function(data){
            console.log(data); // data from the server
            //here you can use the data

         }
         , error: function(jqXHR, textStatus, err){

         }
      })
   });     

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

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