JSON - Permission Error?

时间:2015-02-09 作者:Guit4eva

我有两个下拉列表来过滤网站上的帖子。第二个下拉列表根据第一个下拉列表的选择进行填充,即:

[CATEGORY] + [SUB-CATEGORY] = SEARCH
问题是,有时第二个下拉列表不会填充,控制台显示:

POST: http://localhost/site/wp-admin/admin-ajax.php
{"html":"","error":"Permission error"}
这到底是什么意思?我该如何着手修复它?这是返回错误的代码:

add_action( \'wp_ajax_wpse158929_get_terms_for_cpt\', \'wpse158929_get_terms_for_cpt\' );
add_action( \'wp_ajax_nopriv_wpse158929_get_terms_for_cpt\', \'wpse158929_get_terms_for_cpt\' );

function wpse158929_get_terms_for_cpt() {
$ret = array( \'html\' => \'\', \'error\' => false );

if ( ! check_ajax_referer( \'wpse158929_get_terms_for_cpt_submit_\', \'nonce\', false /*die*/ ) ) {
$ret[\'error\'] = __( \'Permission error\', \'wpfm\' );
} else {
$post_type = isset( $_REQUEST[\'post_type\'] ) ? $_REQUEST[\'post_type\'] : \'\';
$taxonomy = isset( $_REQUEST[\'taxonomy\'] ) ? $_REQUEST[\'taxonomy\'] : \'\';
$current_selected = isset( $_REQUEST[\'current_selected\'] ) ? $_REQUEST[\'current_selected\'] : \'\';

if ( ! $post_type || ! $taxonomy ) {
    $ret[\'error\'] = __( \'Params error\', \'wpfm\' );
} else {
    global $wpdb;
    $sql = $wpdb->prepare( \'SELECT t.slug FROM \' . $wpdb->terms . \' t\'
        . \' JOIN \' . $wpdb->term_taxonomy . \' AS tt ON tt.term_id = t.term_id\'
        . \' JOIN \' . $wpdb->term_relationships . \' AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id\'
        . \' JOIN \' . $wpdb->posts . \' AS p ON p.ID = tr.object_id\'
        . \' WHERE tt.taxonomy = %s AND p.post_type = %s AND p.post_status = %s\'
        . \' GROUP BY t.slug\'
        , $taxonomy, $post_type, \'publish\' );
    $include = $wpdb->get_col($sql);
    $ret[\'html\'] = preg_replace( \'/<\\/?select[^>]*>/\', \'\', my_dropdown_categories( $taxonomy, $current_selected, $include ) );
}
}

wp_send_json( $ret );
}
非常感谢:)

1 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

您的问题是,您正在检查nonce,而nonce已过时。对于在您的案例中使用nonce的必要性,很难做出具体的评论,但一般来说,nonce应该只用于数据提交而不是查询。

结束