最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Wordpress Admin AJAX Serialize

matteradmin5PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far