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

jquery - Wordpress Admin AJAX Serialize

programmeradmin2浏览0评论

I have tried to send serialized data via wordpress admin ajax, but i am getting output in below format instead of serialized format.

reviewerNotes=approved&app_rej_posts=118059

Jquery Ajax Code Below

$.ajax({ 
            type: 'POST',
            dataType: 'json',
            url: ajax_reviewer_object.ajaxurl,
            data: { 
                'action': 'reviewer_action', //calls wp_ajax_nopriv_ajaxlogin
                'post_data': $('#approval_system').serialize()
                },
            success: function(data){
               $("#lrm_msg").html(data.message);
            }
        });

PHP Function Below:

function reviewer_action_callback(){
    $ss = $_POST['post_data'];
    pre($ss);
}

add_action( 'init', 'reviewer_action' );
function reviewer_action() { 
    wp_enqueue_script('ajax-post-script');

    wp_localize_script( 'ajax-post-script', 'ajax_reviewer_object', array( 
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'load_msg' => plugins_url('lyca-draft-approval/assets/img/ajax-loader.gif')
    ));
    //add_action( 'wp_ajax_nopriv_ajax_post', 'ajax_post' );
    add_action( 'wp_ajax_reviewer_action', 'reviewer_action_callback' );
}

I have tried to send serialized data via wordpress admin ajax, but i am getting output in below format instead of serialized format.

reviewerNotes=approved&app_rej_posts=118059

Jquery Ajax Code Below

$.ajax({ 
            type: 'POST',
            dataType: 'json',
            url: ajax_reviewer_object.ajaxurl,
            data: { 
                'action': 'reviewer_action', //calls wp_ajax_nopriv_ajaxlogin
                'post_data': $('#approval_system').serialize()
                },
            success: function(data){
               $("#lrm_msg").html(data.message);
            }
        });

PHP Function Below:

function reviewer_action_callback(){
    $ss = $_POST['post_data'];
    pre($ss);
}

add_action( 'init', 'reviewer_action' );
function reviewer_action() { 
    wp_enqueue_script('ajax-post-script');

    wp_localize_script( 'ajax-post-script', 'ajax_reviewer_object', array( 
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'load_msg' => plugins_url('lyca-draft-approval/assets/img/ajax-loader.gif')
    ));
    //add_action( 'wp_ajax_nopriv_ajax_post', 'ajax_post' );
    add_action( 'wp_ajax_reviewer_action', 'reviewer_action_callback' );
}
Share Improve this question edited Apr 11, 2019 at 11:43 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 11, 2019 at 11:32 RamkumarRamkumar 111 bronze badge 2
  • 2 This is the correct format for .serialize() method. jQuery documentation – nmr Commented Apr 11, 2019 at 12:00
  • What is the pre function? Is there a particular reason not to use JSON or the REST API? – Tom J Nowell Commented Apr 11, 2019 at 12:13
Add a comment  | 

1 Answer 1

Reset to default 1

Instead of relying on bespoke/custom serialising, and the ancient admin AJAX, consider using JSON and the REST API, e.g.:

add_action( 'rest_api_init', function () {
        register_rest_route( 'ramkumar/v1', '/test/', array(
                'methods' => 'POST',
                'callback' => 'ramkumar_rest_test'
        ) );
} );

function ramkumar_rest_test( $request ) {
    $reviewer_notes = $request['reviewer_notes'];
    $result = ....;
    return $result; // gets sent to the browser as JSON, so return an object/array/etc
}

Now we have yoursite/wp-json/ramkumar/v1/test and can poke it like this:

jQuery.ajax({
    url: "/wp-json/ramkumar/v1/test",
    data: { review_note: "hello I'm a reviewer" }
}).done(function( data ) {
    // json decode data and do stuff with it
});

This also has the benefit of being more debuggable, easier to use, etc. The REST API can accept multiple extra parameters telling it how to validate, sanitise, and authenticate the request. It will also tell you in plain english what you did wrong if you make a mistake

发布评论

评论列表(0)

  1. 暂无评论