Pass PHP Array to script through jquery $.ajax() in WordPress

I have a big array() in DOM.

<div id="array"><?php echo serialize($bigArray); ?></div>

I need to pass this data through ajax in my wordpress theme.

$(document).on('click','#somewhere',function(){
        var datas = $('#array').html();
        $.ajax({
            url : ajax_object.ajaxurl,
            type : 'post',
            data : {
                action:'wordpress_action',
                array: datas
            },
            success: function(res) {
                console.log(res);
            }
        });
    })

And in my php script:

add_action('wp_ajax_wordpress_hook', 'my_func');
add_action('wp_ajax_nopriv_wordpress_hook', 'my_func');

function my_func(){
    $data = unserialize($_POST['array']);
    print_r($data);
    die();
}

But this seems not working.

Any suggestion?
How can I pass php array through ajax?
json_encode? php session?

Read more here: Pass PHP Array to script through jquery $.ajax() in WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *