我想在 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 请求
最佳答案
对我来说似乎有两个问题
- 您在
(function (){
下的函数中放置了不必要的制表符
- 您需要使用
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/