Javascript 内爆中断了Ajax调用
因此,我正在进行一个Ajax调用,它将首先检查帖子ID是否已经被投票 目前,我只是在PHP上工作,首先获取post id,如果它是空的,则设置它,如果它不是空的,则追加id 这里的问题:除非我使用内爆或爆炸方法,否则它似乎不会调用javascript。虽然如果我刷新页面,它会记录投票 这是PHP文件。对于Javascript 内爆中断了Ajax调用,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,因此,我正在进行一个Ajax调用,它将首先检查帖子ID是否已经被投票 目前,我只是在PHP上工作,首先获取post id,如果它是空的,则设置它,如果它不是空的,则追加id 这里的问题:除非我使用内爆或爆炸方法,否则它似乎不会调用javascript。虽然如果我刷新页面,它会记录投票 这是PHP文件。对于用户Id,我刚将其设置为我的管理员Id function my_user_vote() { $user_id = 1; $pageVoted = $_REQUEST["post
用户Id
,我刚将其设置为我的管理员Id
function my_user_vote() {
$user_id = 1;
$pageVoted = $_REQUEST["post_id"];
$currentPosts = get_user_meta($user_id, 'pages_voted_on');
if (empty($currentPosts)) {
// Empty create single array
$postsVotedOn[] = $pageVoted;
} else {
$postsVotedOn = explode('|', $currentPosts);
$postsVotedOn[] = $pageVoted;
}
$boo = implode("|", $pageVoted);
update_user_meta( $user_id, 'pages_voted_on', $boo);
if ( !wp_verify_nonce( $_REQUEST['nonce'], "my_user_vote_nonce")) {
exit("No naughty business please");
}
$vote_count = get_post_meta($_REQUEST["post_id"], "votes", true);
$vote_count = ($vote_count == '') ? 0 : $vote_count;
$new_vote_count = $vote_count + 1;
$vote = update_post_meta($_REQUEST["post_id"], "votes", $new_vote_count);
if($vote === false) {
$result['type'] = "error";
$result['vote_count'] = $vote_count;
}
else {
$result['type'] = "success";
$result['vote_count'] = $new_vote_count;
}
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = json_encode($result);
echo $result;
}
else {
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
die();
}
这是javascript
jQuery(document).ready( function() {
jQuery(".user_vote").click( function() {
post_id = jQuery(this).attr("data-post_id")
nonce = jQuery(this).attr("data-nonce")
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "my_user_vote", post_id : post_id, nonce: nonce},
success: function(response) {
if(response.type == "success") {
jQuery(".vote_counter").html("Votes: " + response.vote_count);
jQuery(".voteUpButton").html('<div class="button btnGreen">Thank you!</div>');
alert("Cooommmon");
console.log(response.vote_count);
}
else {
alert("Your vote could not be added")
}
}
})
})
})
jQuery(文档).ready(函数(){
jQuery(“.user\u vote”)。单击(函数(){
post\u id=jQuery(this).attr(“data-post\u id”)
nonce=jQuery(this).attr(“数据nonce”)
jQuery.ajax({
类型:“post”,
数据类型:“json”,
url:myAjax.ajaxurl,
数据:{操作:“我的用户投票”,post\u id:post\u id,nonce:nonce},
成功:功能(响应){
如果(response.type==“success”){
jQuery(“.vote\u counter”).html(“投票:+response.vote\u count”);
jQuery(“.voteUpButton”).html('Thank you!');
警惕(“合作”);
console.log(响应.投票计数);
}
否则{
警报(“无法添加您的投票”)
}
}
})
})
})
我刚刚对您的代码进行了快速测试,发现了两个引发错误的问题:
1.此行:
$currentPosts=get_user_meta($user_id,'pages_voted_on')代码>
应该是
$currentPosts=get\u user\u meta($user\u id,'pages\u voted\u on',true)代码>
2.我相信这句话:
$boo=内爆(“|”,$pageVoted)代码>
应该是
$boo=内爆(“|”,$postsVotedOn)代码>
说明:
如果没有true
参数,则get\u user\u meta
返回一个数组。你不能分解数组。
$pagesvoted
是要添加的页面的id,而$postsVotedOn
是要将其附加到的实际列表
我能问一下为什么我被否决了吗?哦,我的上帝,缺少的是真的。非常感谢。我很高兴能帮上忙。:)对于所有get_*\u元函数,同样的情况也是如此(没有双关缩进)。