即使我的alert($userId)打印出1(这是用户的id),我仍然得到一个错误,就好像userId没有定义一样。知道为什么吗?
$('.postComment').on('click', function(event){
event.preventDefault();
$userId = $("input[name=user_id]").val();
$imageId = $("input[name=image_id]").val();
$comment = $("textarea[name=comment]").val();
alert($userId);
$.ajax({
method: 'POST',
url: urlComment,
data: {userId: userId, imageId: imageId, comment: comment}
}).done(function(){
alert('Works');
})
});
最佳答案
您正在使用 jQuery,$ 在这里有非常不同的用途:
这是您可以尝试的最简洁的方法:
$('.postComment').on('click', function(event){
event.preventDefault();
$.ajax({
method: 'POST',
url: urlComment,
data: {
userId: $("input[name=user_id]").val(),
imageId: $("input[name=image_id]").val(),
comment: $("textarea[name=comment]").val()}
}).done(function(){
alert('Works');
})
});
在您的代码中,您有一个名为 $userId 的变量,并且您使用的是未声明的 userId,这就是您收到错误的原因。
关于javascript - 为什么我收到 "Uncaught ReferenceError: userId is not defined"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555074/