我有一个html链接,在这里(php解释后):
<a href="javascript:void(0)" onclick="vote_comment(28, 1);" class="vote-arrow">
<img id="up-arrow-pic-comment-28" src="http://localhost/wordpress/wp-content/themes/backyard- cures/images/up_arrow_grey.png" alt="vote up" />
</a>
这是它正在调用的函数:
function vote_comment(comment_id, direction) {
$.post('http://localhost/wordpress/wp-content/themes/backyard-cures/backyard_funcs.php',
{voteType: direction, postId: comment_id, type: 'comment'},
'updateIcon(vote_count, direction, comment_id)',
'json');
}
这是backyard_funcs.php:
if (isset($_POST['voteType'], $_POST['postId']) && is_user_logged_in()) {
if($_POST['type'] == 'comment') {
if (!get_user_meta(get_current_user_id(),'byc_comment_votes',true)) {
add_user_meta(get_current_user_id(),'byc_comment_votes',array($_POST['postId'] => $_POST['voteType']),true)
echo json_encode(array('vote_count' => $_POST['voteType'], 'direction' => $_POST['voteType'], 'comment_id' => $_POST['postId']));
}
else {
echo json_encode(array('vote_count' => $_POST['voteType'], 'direction' => $_POST['voteType'], 'comment_id' => $_POST['postId']));
//$old_vote=get_user_meta(get_current_user_id(),'byc_comment_votes['.$_POST[postId].']',true);
}
}
} else {
// bad request/hack attempt
echo 2;
}
我知道没有调用updateIcon函数(应该在成功后调用)。我一直在使用chrome调试器。我知道执行会进入表决_评论功能,然后执行就会死掉(不确定原因)。我没有收到任何控制台错误。
我意识到这个问题可能过于简单或不清楚。预先致歉,对此我是陌生的(网络开发)。请让我知道是否需要其他信息。提前致谢。
最佳答案
第三个参数应该是一个函数,而不是字符串。这是一个对代码进行最少更改的示例。
function vote_comment(comment_id, direction) {
$.post('http://localhost/wordpress/wp-content/themes/backyard-cures/backyard_funcs.php',
{voteType: direction, postId: comment_id, type: 'comment'},
function(data) { updateIcon(data.vote_count, data.direction, data.comment_id) },
'json'
);
}
编辑:添加解析到回调
编辑2:原来OP在使用chrome调试器时遇到问题,我将此图像发送给他以供指导:http://i.imgur.com/vJyWhYw.png
查看ajax请求响应正文后,他能够自行调试问题,事实证明该问题在PHP中。
关于javascript - 实现AJAX发布时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25675450/