Javascript JQuery没有';添加ajax(Node.js,PUG)后无法工作
我一直在尝试向我的博客文章网站添加类似的功能 当我点击like链接时,它应该运行ajax调用。 在我的server.js文件中,我有一个函数,用于接收mongodb数据库中给定帖子的帖子请求更新次数。 我已经将链接的name属性设置为equal post_id,以便可以在数据库中修改该帖子Javascript JQuery没有';添加ajax(Node.js,PUG)后无法工作,javascript,jquery,node.js,ajax,pug,Javascript,Jquery,Node.js,Ajax,Pug,我一直在尝试向我的博客文章网站添加类似的功能 当我点击like链接时,它应该运行ajax调用。 在我的server.js文件中,我有一个函数,用于接收mongodb数据库中给定帖子的帖子请求更新次数。 我已经将链接的name属性设置为equal post_id,以便可以在数据库中修改该帖子 html head style include ../styles/home.css link(rel='stylesheet', href='https://cdnjs.c
html
head
style
include ../styles/home.css
link(rel='stylesheet', href='https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css')
script(src='https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js')
script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js')
script.
$(document).ready(function(){
$('.number_of_likes').click(function(e){
e.preventDefault();
alert('This part wont run');
$.ajax(
type: 'POST',
url: '/like',
data: {'post_id' : $(this).attr('name')},
success: function(result){
alert('it works');
},
dataType: 'json'
);
});
});
h2 Welcome to the main page
br
br
br
br
br
mixin postCard(postData)
.post_container
.row
.col.s12.m6
.card.blue-grey.darken-1
.card-content.white-text
span.card-title #{postData.title}
p= postData.content
.card-action
a(href='#') #{postData.username}
a(href='/delete_post/'+postData._id) Delete
a(href='' name=postData_id class='number_of_likes') #{postData.likes}
each post in result
+postCard(post)
但不会触发click事件。问题可能是因为这是无效语法,导致整个应用程序停止:
$.ajax(
type: 'POST',
url: '/like',
data: {'post_id' : $(this).attr('name')},
success: function(result){
alert('it works');
},
dataType: 'json'
);
您需要将其作为一个对象传递,其数据介于{
和}
之间,如下所示:
$.ajax({
type: 'POST',
url: '/like',
data: {'post_id' : $(this).attr('name')},
success: function(result){
alert('it works');
},
dataType: 'json'
});
问题可能是因为这是导致整个应用程序停止的无效语法:
$.ajax(
type: 'POST',
url: '/like',
data: {'post_id' : $(this).attr('name')},
success: function(result){
alert('it works');
},
dataType: 'json'
);
您需要将其作为一个对象传递,其数据介于{
和}
之间,如下所示:
$.ajax({
type: 'POST',
url: '/like',
data: {'post_id' : $(this).attr('name')},
success: function(result){
alert('it works');
},
dataType: 'json'
});