如何对我的php函数使用get请求AJAX调用?

时间:2017-10-26 09:35:57

标签: javascript php ajax wordpress get

对不起,如果这看起来像一个荒谬的问题,但是,我已经把头发拉了3天试图弄清楚如何做到这一点...

我基本上想要提取这个(查询var?)并使用当前函数中的参数...

?vote=up&comment_id=8

这是我目前的职能:

<?php
// This file is used for buildimng the custom comment upvote and downvote functionality.


add_action( 'wp_enqueue_scripts', 'ajax_enqueue' );
function ajax_enqueue() {
// Enqueue File from 'inc' folder.
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/inc/ajax/comment_vote_ajax.js', array('jquery') );

// in JavaScript, object properties are accessed as         
ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
    array(  
        'ajax_url' => admin_url( 'admin-ajax.php' ), 
        'user_id' => get_current_user_id()
    )
);
}

// Same handler function...
function ajax_action() {
global $wpdb;

if ( ! is_user_logged_in() ) {
    return;
}

if ( ! isset( $_GET[ 'vote' ] ) && ! isset( $_GET[ 'comment_id' ] ) ) {
    return;
}

$up_or_down = esc_attr( $_GET[ 'vote' ]) ;
if ( ! in_array( $up_or_down, [ 'up', 'down' ] ) ) {
    return;
}

$comment_id = (int) esc_attr( $_GET[ 'comment_id' ] );
if ( $comment_id < 1 ) {
    return;
}

// Retreive the existing comment up/down vote ID's - we don't know who did what at this point..
$vote_user_ids = get_comment_meta( $comment_id, '_vote_user_ids', true );

// If the comment upvote count isn't set, create an empty array.
// This will always be the case on the first instance of a user "upvoting" a comment
if ( ! is_array( $vote_user_ids ) ) {
    $vote_user_ids = [];
}

// Check if the user has already "voted"
if ( array_key_exists( get_current_user_id(), $vote_user_ids ) ) {

    // If the user HAS voted and they are repeating themselves (e.g. the voted up and are trying to do this again) ignore them..
    if ( $up_or_down === $vote_user_ids[ $user_id ] ) {
        return;
    } else {
        // The user voted already but have changed their vote - update this
        //$vote_user_ids[ get_current_user_id() ] = $up_or_down;
        unset ( $vote_user_ids[ get_current_user_id() ] );
    }

} else {

    // The user HAS NOT voted, add them
    $vote_user_ids[ get_current_user_id() ] = $up_or_down;
}

// Whatever the results of ^^ we need to update now
$update_result = update_comment_meta( $comment_id, '_vote_user_ids', $vote_user_ids );
delete_transient( "comment_vote_count_comment_{$comment_id}" );


if ( ! $update_result ) {
    error_log( "Error updating comment meta to update $comment_id to set user ID $user_id $up_or_down." );
}

wp_die();
}

add_action( 'wp_ajax_my_action', 'ajax_action' );
add_action( 'wp_ajax_nopriv_my_action', 'ajax_action' );

// ajax调用结束。 这是我目前的剧本......

var $ = jQuery.noConflict();

function getParameterByName(name, url) {
if (!url) url = $(this).attr('href');
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}

$(document).ready(function(){

$("a.upvote").click(function(e) {
    e.preventDefault();
    var comment_id = getParameterByName('comment_id', $(this).attr('href'));

    filters_get = {
        'vote': 'up',
        'comment_id': comment_id
    };

    $.ajax({
      url: ajax_object.ajax_url,
      type: "post", //send it through get method
      data: filters_get,
      cache: false,
      success: function(data) {
        console.log( 'comment_id' + ' = ' + comment_id );
        console.dir(data);
        console.log(data);
        $('.comment p').after(data);
      },
      error: function(xhr) {
      }
    });
});
});

0 个答案:

没有答案