wp was extended with many things, including
wp.ajax. This combined with the
wp_send_json_error() helper functions can help make performing ajax requests with the
admin-ajax.php processor very straight forward.
If you are not already familiar with the
admin-ajax.php processor, it’s worth learning a little more about. It is not and should not be the only way you work with Ajax in WordPress, but it’s a very useful tool to have at your disposal and should not be overlooked.
Let’s start by defining what
wp.ajax is and does. It contains two methods currently,
wp.ajax.send(). I’m going to focus this article on
wp.ajax.post() is really just a wrapper around the send method that ensures the request is sent as a POST request. Beyond the request type, both
.post work almost the same.