最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - WordPress Ajax request "Failed to load resource: the server responded with a status of 400 ()"

programmeradmin0浏览0评论

I spend a lot of time searching for a solution to this issue. I looked on google and stackexchange but didn't get the answer.

So I am looking for the solution is while making an ajax request through my plugin(that I am developing) i am getting this 400 status error. I don't know why i am getting this?

1 Scripts

function smn_admin_assets($page_now){    
    if( $page_now == "settings_page_smn_notice" ){
        wp_enqueue_style( 'smn-admin-style', plugins_url( "../assets/admin/admin.css", __FILE__ ) );
        wp_enqueue_script( 'smn-admin-js', plugins_url( "../assets/admin/admin.js", __FILE__), array( 'jquery' ), VERSION, true  );        
        $pass_value = array(
            'url'            => admin_url( 'admin-ajax.php' ),
            'cookieNonce'    => wp_create_nonce('cookieNonce')
        );
        wp_localize_script('smn-admin-js', 'smn_ajax', $pass_value );
    }    
}
add_action( 'admin_enqueue_scripts', 'smn_admin_assets' );

2 Ajax Request

$(document).ready(function () {        

    $("#clearCookie").on('click', function(){
        var clearCookie = $('#clearCookie').val();
        // Ajax Call
        $.ajax({
            url : smn_ajax.url,
            type : 'POST',
            data : {
                action : 'smn_clear_cookie',
                clearcookie : clearCookie
            },
            success : function( response ) {
                alert('Hello');
            },
            error: function () {
                alert(smn_ajax.url);
            }
        });

        return  false;
    });
});

3 Ajax Handling

function smn_clearCookie(){

    $clearCookie = $_POST['clearCookie'];
    var_dump($clearCookie);

    die();
}
add_action( 'wp_ajax_smn_clear_cookie', 'smn_clearCookie' );
add_action( 'wp_ajax_nopriv_smn_clear_cookie', 'smn_clearCookie' );

Let me know why i am getting this error; Thank you for your time.

发布评论

评论列表(0)

  1. 暂无评论