且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

400 错误请求与 Wordpress AJAX 调用

更新时间:2022-06-27 07:05:52

请替换代码并检查

plugin.php

function my_admin_scripts() {
    $localize = array(
        'ajaxurl' => admin_url( 'admin-ajax.php' )
    );

    wp_enqueue_script( 'veh-app-search', plugin_dir_url( __FILE__ ) . '/ajax.js', array( 'jquery' ) );

    wp_localize_script( 'veh-app-search', 'veh_app_script', $localize);


}  

add_action( 'wp_enqueue_scripts', 'my_admin_scripts' );
add_action( 'wp_ajax_handle_request', 'handle_request' );
add_action( 'wp_ajax_nopriv_handle_request', 'handle_request' ); 

    //takes care of the $_POST data
function handle_request(){
    echo "hello";
}

ajax.js

var data = {
        action: 'handle_request',
        RequestType: 'category',
        Category:  jQuery('#Category option:selected').val()
    };

    jQuery.post(
        veh_app_script.ajaxurl,
        data,
        function(categories){
            console.log(categories);
        }
    );