在我的自定义主题文件夹上的function.php文件中,我有这个:
function getPrices(){
$price = get_post_meta($_REQUEST["post_id"], "price_one", true);
$results['price'] = $price;
$results = json_encode($results);
die($results);
}
add_action('wp_ajax_getPrices', 'getPrices');
add_action('wp_ajax_nopriv_getPrices', 'getPrices');
在js文件中我有这个:
$('ul.people_adult li').click(function(){
var post_id = $("#post_id").val();
jQuery.ajax({
type:"POST",
dataType : "json"
url: "http://localhost/wordpress/wp-admin/admin-ajax.php",
data: {
action: 'getPrices',
post_id: post_id
},
complete:function(data){
console.log(data.price);
}
});
});
当我单击以下错误消息时:
POST http://localhost/wp-admin/admin-ajax.php 404 (Not Found)
此外,当我提醒返回的数据时,它显示对象Object但是当我将其记录到控制台时,它会显示响应页面的所有html代码。
任何想法??
谢谢!
答案 0 :(得分:1)
最大的问题是我提供的网址不好,并且出于某些原因,直到杰克逊和康施施特提及它才会注意到。
我的function.php文件中也出现了拼写错误。
以下是我的问题的答案。
function getPrices(){
$the_id = $_REQUEST["post_id"];
$results[] = get_post_meta($the_id, "price_one", true);
$results[] = get_post_meta($the_id, "price_two", true);
$results[] = get_post_meta($the_id, "price_three", true);
$results[] = get_post_meta($the_id, "price_four", true);
$results[] = get_post_meta($the_id, "price_five", true);
$results[] = get_post_meta($the_id, "price_six", true);
$results[] = get_post_meta($the_id, "price_seven", true);
$results[] = get_post_meta($the_id, "price_eight", true);
$results = json_encode($results);
die($results);
}
add_action('wp_ajax_getPrices', 'getPrices');
add_action('wp_ajax_nopriv_getPrices', 'getPrices');
和js文件
$('ul.people_adult li').click(function(){
var post_id = $("#post_id").val();
jQuery.ajax({
type:"POST",
dataType : "json",
url: "http://localhost/wordpress/wp-admin/admin-ajax.php",
data: {
action: 'getPrices',
post_id: post_id
},
success:function(response){
$("#price_est_person").html(response[0]);
/*Do some stuff with response[1], response[2 etc]*/
}
});
});