How to pass data parameter to ajax action function handler

When you use jQuery.post(), the data is sent as regular $_POST arguments.

So this JavaScript …

var data = {
    action: 'load_post',
    foo:    'bar'
};

… is available in your callback function per:

$action = $_POST['action'];
$foo    = $_POST['foo']; // bar

And when you are using jQuery.get(), the data is in $_GET. You can also use $_REQUEST which returns both, GET and POST data (and COOKIE). But you should always ask for the specific resource to avoid injection from sources you didn’t expect, like a cookie.

Leave a Comment