简体   繁体   English

在玉环中发送帖子

[英]Send post in jade loop

i'm having some difficulties sending a post request with a specific ID in Node and Jade. 我在Node和Jade中发送带有特定ID的帖子请求时遇到一些困难。

Say I have Node returning a list of books : 假设我让Node返回书籍清单:

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

My Jade template shows a list of all the books by looping through them. 我的Jade模板通过循环浏览显示所有书籍的列表。

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 ? 如何在发帖请求中将book对象的ID发送给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): 您可以将图书ID与每个DOM元素相关联(一个按钮或将要使用的任何按钮):

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): 然后绑定一个单击处理程序,该处理程序将触发POST请求(假设您的后端使用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'
  });
});

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

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