weixin_33743880 2015-02-14 20:43 采纳率: 0%
浏览 5

内爆中断Ajax通话

So I'm making an Ajax call which will first check to see if that post ID has already been voted on.

Currently I'm just working on the PHP to first get the post id's, if it is empty set it or if it is not empty to append the ID.

Question here: Except when I use the implode or explode method it does not seem to make a call back to the javascript. Although if I was to refresh the page it does register the vote.

This is the PHP file. For user Id I've just set it to my admin id to start with.

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();

}

This is the 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")
            }
         }
      })   

   })

})
  • 写回答

1条回答 默认 最新

  • Memor.の 2015-02-14 21:24
    关注

    I just did a quick test with your code, and found a couple of issues that throw errors:

    1. This line:

    $currentPosts = get_user_meta($user_id, 'pages_voted_on');

    should be

    $currentPosts = get_user_meta($user_id, 'pages_voted_on', true);

    2. And I believe this line:

    $boo = implode("|", $pageVoted);

    should be

    $boo = implode("|", $postsVotedOn);

    Explanation:

    1. Without the true argument get_user_meta returns an array. And you can't explode an array. http://codex.wordpress.org/Function_Reference/get_user_meta
    2. $pageVoted is the id of the page to add, while $postsVotedOn is the actual list you want it appended to.
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程