jquery - Wordpress Admin AJAX Serialize

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745600714a4635384.html

相关推荐

  • jquery - Wordpress Admin AJAX Serialize

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

    14小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信