【问题标题】:stop page reloading when using ajax使用ajax时停止页面重新加载
【发布时间】:2015-03-12 11:13:56
【问题描述】:

我使用 ajax 创建了一个 wordpress 插件来对帖子进行投票。 当您单击“投票”链接时,jquery 弹出窗口有效,您的投票已添加,但页面会重新加载

    add_action("wp_ajax_my_user_vote", "my_user_vote");
    add_action("wp_ajax_nopriv_my_user_vote", "my_must_login");

    function my_user_vote() {

    if ( !wp_verify_nonce( $_REQUEST['nonce'], "my_user_vote_nonce")) {
        exit("No naughty business please");
    }   


    $user_id = get_current_user_id();

    date_default_timezone_set('GMT+2');
    $dateVoted = get_user_meta($user_id, 'date');
    $today = date('d M Y');
    if ($dateVoted === $today){
        //Already Voted
        echo '<script language="javascript">';
        echo 'alert("already voted")';
        /*
        echo 'alert("User ID: ' . $user_id . '")';
        echo 'alert("Date Voted: ' . $dateVoted . '")';
        echo 'alert("Today: ' . $today . '")';
        */ 
        echo '</script>';

    }else{

       $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"]);
       }

       update_user_meta( $user_id, 'date', $today );

    }
    die();

}


function my_must_login() {
   echo "You must log in to vote";
   die();
    }

    add_action( 'init', 'my_script_enqueuer' );

    function my_script_enqueuer() {
       wp_register_script( "my_voter_script", WP_PLUGIN_URL.'/video-of-the-     day/my_voter_script.js', array('jquery') );
       wp_localize_script( 'my_voter_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        

        wp_enqueue_script( 'jquery' );
       wp_enqueue_script( 'my_voter_script' );

    }

我还有一个 jquery 文件:

 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") {

            }
            else {
               alert("Your vote could not be added")
            }
         }
      });   

   })

    });

为什么 ajax 不起作用?

【问题讨论】:

    标签: ajax wordpress


    【解决方案1】:

    试试这个

    jQuery(".user_vote").click( function(e) {
      e.PreventDefault();
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 1970-01-01
      • 2018-11-01
      • 2019-06-24
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      相关资源
      最近更新 更多