【发布时间】:2016-02-08 15:34:52
【问题描述】:
我正在开发一个 WordPress 主题,当用户将鼠标悬停在帖子预览上时,我需要在前端显示一些信息。我使用 AJAX 来检索所需的信息,而不是为每个帖子加载额外的数据,但我所有的 AJAX 查询都返回 0。
这是我的functions.php的摘录:
add_action( 'wp_ajax_nopriv_pgsc_ajax_get_supporters', 'pgsc_ajax_get_supporters' );
add_action( 'wp_ajax_pgsc_ajax_get_supportes', 'pgsc_ajax_get_supporters' );
function pgsc_ajax_get_supporters()
{
$politicianId = $_POST['postId'];
$field = get_field_object('lista', $postId);
$values = get_field("lista", $postId);
$lists = array();
foreach($values as $val) {
$listName = $field["choices"][$val];
$logo = get_field("logo", $val)["sizes"]["thumbnail"];
$lists[$listName] = array("permalink" => get_post_permalink($val), "logo" => $logo);
}
wp_reset_query();
echo json_encode($lists);
wp_die();
}
这是来自我的 JavaScript 的 AJAX 调用:
function open_supporter_bar(postId, barId)
{
var bar = "#bar_" + barId;
jQuery(bar).hide("slow");
jQuery(bar).html("");
jQuery.ajax({
url : pgsc.ajaxurl,
type : "POST",
data : {
action : "pgsc_ajax_get_supporters",
postId : postId
},
success : function(result) {
alert(result);
html = compose_supporter_list(result);
jQuery(bar).html(html);
jQuery(bar).show("slow");
},
error : function(error) {
alert("Check the error log!");
}
});
}
我已经尝试用die() 替换wp_die(),但结果是一样的
【问题讨论】:
-
你在调试请求吗?你能打开你的开发工具(firefox、chrome、IE 等)并查看网络选项卡会发生什么吗?有服务器错误?你能在你的php中创建一个
var_dump($lists),看看它是否出现在调试器中吗? -
我尝试从我的 PHP 函数中记录
$lists,但我的日志文件中没有出现任何内容。从浏览器的行为来看,一切都很好(JS 中的错误函数永远不会被调用),但这就像我的 PHP 函数根本不会被调用......