我制作了一个wordpress ajax调用代码,其中当我点击一个按钮时,该函数正常运行并且第一次返回该值,但在此之后ajax调用admin-ajax.php keepon运行,因为我可以看到来自开发者工具。我的代码是
add_action( 'wp_ajax_arete_bp_endorse', 'arete_bp_endorse_ajax' );
add_action( 'wp_ajax_nopriv_arete_bp_endorse', 'arete_bp_endorse_ajax' );
function arete_bp_endorse_ajax(){
global $wpdb;
$result['type'] = "works";
echo $result;
wp_die();
}
function my_script_enqueuer() {
wp_register_script( "my_voter_script", WP_PLUGIN_URL.'/example/js/example.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' );
}
example.js文件代码
jQuery(document).on("click",".endorse-bt", function (event){
event.preventDefault();
var type= jQuery(this).attr("data");
var option= jQuery(this).parents("li").attr("data-endorsed-item-name");
var profile_id=jQuery(this).parents("li").attr("data-prf-id");
var login_id=jQuery(this).parents("li").attr("data-lg-id");
jQuery.ajax({
dataType : "json",
url : myAjax.ajaxurl,
type: 'POST',
data:{action: "arete_bp_endorse", type : type, option: option, profile_id: profile_id, login_id: login_id},
success: function( response ){
//Do something with the result from server
alert(response.type);
}
});
});