send data from ajax GET to ejs file

耗尽温柔 提交于 2020-01-25 09:42:07

问题


I am retrieving some user data with an ajax GET call, and want to display each user in a bootstrap card, so that I can filter these on the page using jQuery.

Currently, I get the data, iterate over each user and append some card elements to the <div class="card-deck"></div>:

jQuery:

$(document).ready(function() {
    $.getJSON('/api/user/all')
        .then(data => {
            $.each(data, function (i, user) {
                var userCard = '<div class="col-md-auto mb-4">' +
                '<div class="card matches mx-auto" style="width: 18rem; height: 24rem;">' +

                '<div class="card-body">' +
                    '<h5 class="card-title">' + user.username + '</h5>' +
                    '<p class="card-text">'   + user.jobTitle + '</p>' +
                    '<p class="card-text">'   + user.city + '</p>' +
                '</div>' +

                "</div>" +
                "</div>";

                $('#userList').append(userCard);
            });
        })
})

ejs:

<div class="row">
    <div class="card-container align-items-left">
        <div class="card-deck" id="userList">
            // cards go here ... 
        </div>
    </div>
</div>

This works, but there will be a lot of html that goes into building the cards, so I would prefer to send the entire user object (below this is user) to the .ejs file, so that I can build the card there:

<div class="row">
    <div class="card-container align-items-left">
        <div class="card-deck" id="userList">

          <div class="col-md-auto mb-4">
              <div class="card matches mx-auto" style="width: 18rem; height: 24rem;">
                  <div class="card-body">
                      <h5 class="card-title"><%=user.username%></h5>
                      <p class="card-text"><%=user.jobTitle%></p>
                      <p class="card-text"><%=user.city%></p>
                  </div>
              </div>
          </div>

        </div>
    </div>
</div>

Is this a job fo the jQuery data method?


回答1:


You need to implement an endpoint that fetches user information and forms the template for your userList div and sends the data back as a plain html string.

This endpoint has to be called from the client via an ajax call and set the response html to the div

Server

    app.get('/api/user/all',(req, res){
   //get user data
   const data = [{username:"john",jobTitle:"a",city:"b"},{username:"doe",jobTitle:"a",city:"b"}];

   res.render('userTemplate', {users:data} ,function(err, html) {
      res.send(html);
  });

Client

$.ajax({
  url: "/api/user/all",
  cache: false,
  success: function(html){
    $("#userList").innerHtml(html);
  }
});

userTemplate.ejs

<% for(var i=0; i < users.length; i++) { %>
    <div class="col-md-auto mb-4">
        <div class="card matches mx-auto" style="width: 18rem; height: 24rem;">
            <div class="card-body">
                <h5 class="card-title"><%= users[i].username %></h5>
                <p class="card-text"><%= users[i].jobTitle %></p>
                <p class="card-text"><%= users[i].city %></p>
            </div>
        </div>
    </div>
 <% } %>



回答2:


EJS is server-side code.

If you want to generate your HTML server side instead of modifying the DOM client side, then replace getJSON with an ajax call (with dataType: "html") and change /api/user/all to an endpoint (which you will have to create) which gets the data and inserts it into an EJS template.



来源:https://stackoverflow.com/questions/55592172/send-data-from-ajax-get-to-ejs-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!