0
我試圖得到一個Ajax函數與wordpress一起工作,總是得到默認結果。 我在的functions.php(其他功能旁)WordPress的AJAX無法讓它工作總是返回0
add_action('wp_ajax_count_results_cal', 'count_results');
add_action('wp_ajax_nopriv_count_results_cal', 'count_results');
function count_results(){
echo 'test';
die();
}
function my_theme_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('theme-plugins',get_template_directory_uri() . '/js/plugins.js' , array('jquery','jquery-ui-draggable','jquery-ui-dialog'));
wp_enqueue_script('google-map','http://maps.google.com/maps/api/js?sensor=true', array('jquery','jquery-ui-draggable','jquery-ui-dialog'));
wp_enqueue_script('theme-script', get_template_directory_uri() . '/js/script.js', array('jquery','jquery-ui-draggable','jquery-ui-dialog'));
wp_enqueue_script('ajax', get_template_directory_uri() . '/js/ajax.js', array('jquery'));
}
add_action('init', 'my_theme_scripts');
在我ajax.js文件
jQuery(document).ready(function ($) {
$('#manufacturer,#make_date,#fuel_type,#transmission,#price_from,#price_to').on({
change: function(){
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'count_results_cal',
manufacturer : $("#manufacturer").val(),
year : $("#make_date").val(),
fuel : $("#fuel_type").val(),
transmission : $("#transmission").val(),
price_from: $("#price_from").val(),
price_to : $("#price_to").val(),
body_type : $("#body_type").val(),
vat : $("#vat").val()
},
success: function(data, textStatus, XMLHttpRequest){
$('#offers_found span').html(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
}});
}); 我試過了所有我能想到的沒有結果的東西。有沒有人有一些想法?