因此,我已经阅读了一些关于ajax的教程和关于它的codex文档,我想我现在已经理解了它应该如何工作,但是wp\\U localize\\U脚本似乎什么都没有做。根据我在codex和各种论坛上读到的内容,wp\\u localize\\u脚本应该允许我创建一个外部javascript文件可以引用的全局对象。但当我在chrome中检查DOM时,它不在那里。我的代码如下
class Admin_Page extends Admin_Console {
static $add_script;
public static function init() {
add_shortcode(\'CLADMIN\', array(__CLASS__, \'handle_shortcode\'));
add_action(\'init\', array(__CLASS__, \'register_script\'));
add_action(\'wp_footer\', array(__CLASS__, \'print_script\'));
// add_action( \'wp_ajax_getData\', \'data_response\' );
}
static function data_response() {
//do stuff
}
public function register_script() {
wp_enqueue_script(\'masterJS\',\'/wp-content/plugins/AdminQuizConsole/js/master.js\', array(\'jquery\',\'jquery-ui-core\',\'jquery-effects-core\'), true);
wp_register_style( \'master\', \'/wp-content/plugins/AdminQuizConsole/css/master.css\' );
}
static function print_script() {
if ( ! self::$add_script )
return;
wp_enqueue_style(\'master\');
wp_localize_script(\'masterJS\', \'AjaxRequest\', array( \'ajaxurl\' => admin_url(\'admin-ajax.php\') ) );
}
}
Admin_Page::init();
现在,除非我根据我在这里看到的内容弄错了,否则应该在排队之后调用本地化,因此应该向DOM中添加类似的内容
/* <![CDATA[ */
var MyAjax = {
ajaxRequest: "http://example.com/wordpress/wp-admin/admin-ajax.php"
};
/* ]]> */
但这似乎没有发生。有人能给我解释一下我遗漏了什么吗?
EDIT(编辑)解决了上述问题,但管理页面现在返回零
public static function init() {
add_shortcode(\'CLADMIN\', array(__CLASS__, \'handle_shortcode\'));
add_action(\'wp_enqueue_scripts\', array(__CLASS__, \'register_script\'));
// add_action(\'wp_footer\', array(__CLASS__, \'print_script\'));
add_action( \'wp_ajax_nopriv_get_data\', \'data_response\' );
}
public function data_response() {
global $wpdb;
$user_id=null;
$user_id = $user_id = $user_id ? $user_id : get_current_user_id();
$practiceKey = \'user_practice\';
//Get Practice
$sql = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = %s", $user_id, $practiceKey );
$sql3 = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = %s", $user_id, \'user_practice_level\' );
$practice = $wpdb->get_var( $sql );
$usr_level = $wpdb->get_var( $sql3 );
$db_name = $wpdb->prefix . \'plugin_slickquiz_scores\';
if($usr_level == 0){
$get_data = $wpdb->prepare( "SELECT quiz_id,score,createdDate FROM $db_name WHERE usr = $user_id" );
$results = $wpdb->get_results($get_data,ARRAY_A);
$result_array = array();
foreach ( $results as $value )
{
$result_array[] = $value;
}
} elseif ($usr_level == 1) {
$get_data = $wpdb->prepare( "SELECT * FROM $db_name WHERE usr_practice = %s",$practice );
$results = $wpdb->get_results($get_data,ARRAY_A);
foreach ( $results as $value )
{
$result_array[] = $value;
}
} else {
$result_array[] = (1);
}
$data_encode = json_encode($result_array);
header( "Content-Type: application/json" );
echo $data_encode;
die();
}
function get_level() {
$.post(AjaxRequest.ajaxurl, {action : "get_data"})
.done(function( json ){
console.log(json);
})
.fail(function( jqxhr, textStatus, error){
var err = textStatus + \', \' + error;
console.log( \'Request Failed: \' + err);
});
}
动作名称和回调函数似乎匹配,并且正在通过post发送,以防我想要/需要更改no\\u priv选项。
我确信这只是我缺少的东西,但我已经测试了我的输出,将查询从回调函数中取出,并将其直接返回到页面上,它确实返回了有效的JSON,所以我不确定我现在缺少了什么。