javascript - 如何在 pug 模板中发送 Ajax 请求?

标签 javascript ajax pug

我想在 pug 模板文件中发送 Ajax 请求。

form#payment-form()
  section
    label(for="amount")
      input#amount(name="amount" type="tel" min="1" placeholder="Amount"                         value="10")
script(src="scripts/jquery.js")
script.
  (function () {
    var amount = $('#amount').val();
     $.ajax({
      type: "POST",
      url: "/posturl",
      data: {'amount':amount},
      success: function(){},
      dataType: 'json'
    });
  })()

但是不行,怎么办? 我想知道如何在 pug 文件的嵌入式 javascript 中发送 ajax 请求

最佳答案

对我来说似乎有两个问题

  1. 您在(function (){
  2. 下的函数中放置了不必要的制表符
  3. 您需要使用 document.ready 来确保 HTML 内容是 准备好。如果您在收到响应后并不真正关心 DOM,则可以避免这种情况

检查下面的工作示例

doctype html
html
  head
    script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js')
    script.
      $(document).ready(function(){
      $.ajax({url: "demo_test.txt", success: function(result){
      $("#div1").html(result);
      }});
      });
  body
    #div1
      h2 Let jQuery AJAX Change This Text

关于javascript - 如何在 pug 模板中发送 Ajax 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42674607/

相关文章:

javascript - Angular - 在本地文本文件中保存 blob?

javascript - Contenteditable 高度动画 : animate only after a line is deleted

javascript - Window.Location.Href 或其他重定向 _self 的方法不起作用(MVC View )

jquery getJSON 对 cfc 的调用可以在测试服务器上运行,但不能实时运行

validation - Node.js、Express 和 Jade - 表单

javascript - 云函数错误: Converting circular structure to JSON

javascript - 从对象数组传递某些参数

javascript - 使用 jQuery 重新加载页面,同时保留一些数据

html - 不希望包含标记的样式表与当前页面的样式表混合

javascript - nodejs peepcode 教程 - 无法让它工作