简体   繁体   中英

Send post in jade loop

i'm having some difficulties sending a post request with a specific ID in Node and Jade.

Say I have Node returning a list of books :

  res.render('tests', {books: books});

My Jade template shows a list of all the books by looping through them.

block content
  .page-header
    h3 All Books

  ul
    for book in books
      li= book.title

I now want a 'LIKE' button for every book. How can I send the ID of the book object in a post request to fe http://example.com/books/like ?

You could associate the book id to each DOM element (a button or whatever you are going to use):

ul
  for book in books
    li(class='like-book', id=book.id) book.title

And then bind a click handler that will trigger the POST request (assuming your backend speaks JSON):

$('.like-book').on('click', function(evt) {
  var data = JSON.stringify({
    id: evt.target.id,
    // ...
  });

  $.ajax({
    type: "POST",
    url: "http://example.com/books/like",
    data: data,
    success: function(result) { /* */ },
    error: function(jqXHR, status, error) { /* */ },
    dataType: 'json'
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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