【发布时间】:2017-10-08 05:44:39
【问题描述】:
我正在通过 howcode 在 youtube 上关注一系列教程。它是关于建立一个社交网络。当谈到喜欢/评论时,他使用了一个 api。但是,这对我不起作用,我想使用普通的 ajax。我想提交一个类似的请求:
<form>
if (!DB::query('SELECT post_id FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$post['id'], ':userid'=>$userid))) {
echo "<input type='submit' name='like' value='Like' data-id=".$post['id'].">";
} else {
echo "<input type='submit' name='unlike' value='Unlike' data-id=".$post['id'].">";
}
echo "<span>".$post['likes']." likes</span>
</form>
到达这个 ajax:
$('[data-id]').click(function() {
var buttonid = $(this).attr('data-id');
$.ajax({
type: "POST",
url: "index.php?postid=" + $(this).attr('data-id'),
processData: false,
//ScontentType: "application/json",
data: '',
success: function(r) {
console.log("hhhhhhhhhhhhhhhh")
},
error: function(r) {
console.log(r)
}
});
});
然后去这里:
if (isset($_GET['postid'])) {
Post::likePost($_GET['postid'], $userid);
}
【问题讨论】:
-
我不知道我哪里出错了
-
错误是什么?
-
jquery-1.11.1.min.js:2 Uncaught SyntaxError: Unexpected token ) at jquery-1.11.1.min.js:2 at Function.globalEval (jquery -1.11.1.min.js:2) 在文本脚本 (jquery-1.11.1.min.js:4) 在 PC (jquery-1.11.1.min.js:4) 在 x (jquery-1.11.1 .min.js:4) 在 b (jquery-1.11.1.min.js:4) 在 Object.send (jquery-1.11.1.min.js:4) 在 Function.ajax (jquery-1.11.1. min.js:4) 在 Function.m._evalUrl (jquery-1.11.1.min.js:4)
-
您的选择器语法肯定是错误之一,因此您的点击功能将无法正常工作。
-
你如何包含 jQuery 库?
标签: javascript php jquery ajax pdo