免責事項:これがWordPressに関連しているかどうかはわかりません。
簡単なチュートリアルに従って、WordPressローカルホストでAJAXが機能するかどうかを確認しています。
私のajax-test.js:
jQuery(document).ready( function($){
$(".ajax-link").click( function(){
var data = {
action: 'test_response',
post_var: 'this will be echoed back'
};
$.post(the_ajax_script.ajaxurl, data, function(response) {
alert(response);
});
return false;
});
});
プラグインで、スクリプトをwp_print_scripts()に追加し、処理関数を追加します。
function test_ajax_load_scripts(){
wp_enqueue_script("ajax-test", plugin_dir_url( __FILE__ ) . 'ajax-test.js', array( 'jquery' ) );
wp_localize_script('ajax-test', 'the_ajax_script', array( 'ajaxurl' => admin_url('admin-ajax.php') ) );
}
add_action('wp_print_scripts', 'test_ajax_load_scripts');
function text_ajax_process_request(){
if(isset($_POST["post_var"])){
$response = $_POST["post_var"];
echo $response;
die();
}
}
add_action('wp_ajax_test_response', 'test_ajax_process_request');
href = "#"のリンク出力を取得し、それをクリックすると、ページの上部に移動します。Firebugをチェックすると、ajax-test.jsがヘッダーにロードされていることを確認できます。コンソールには何もありません。
アラートが発生しない理由を他にどのようにデバッグできますか?