我想在wordpress中发送一个ajax请求,跟踪我的点击。目前,我已将其添加到我的函数文件中:
add_action(\'init\', \'my_script_enqueuer\');
function my_script_enqueuer() {
wp_register_script("history_script", get_template_directory_uri() . \'/js/history_script.js\', array(\'jquery\'));
wp_localize_script(\'history_script\', \'myAjax\', array(\'ajaxurl\' => get_template_directory_uri().\'/functions.php\'));
wp_enqueue_script(\'jquery\');
wp_enqueue_script(\'history_script\');
}
add_action("wp_ajax_history_trace", "history_trace");
function history_trace() {
echo \'fasfasgasgas\'; die;
}
在我的js文件中:
jQuery(document).ready( function() {
jQuery("#searchsubmit").click( function() {
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "history_trace"},
success: function(response) {
if(response.type == "success") {
alert(\'success\')
}
else {
alert("false")
}
}
})
})
})
但当我看到我的控制台时,rquest显示为红色,没有响应。请帮忙!!
SO网友:Monirul Islam
我认为你的ajax url是错误的。ajax链接应该是admin\\u url(\'admin ajax.php\')。这意味着
wp_localize_script(\'history_script\', \'myAjax\', array(\'ajaxurl\' => get_template_directory_uri().\'/functions.php\'));
就是这个
wp_localize_script(\'history_script\', \'myAjax\', array(\'ajaxurl\' => admin_url( \'admin-ajax.php\' ));