-
Notifications
You must be signed in to change notification settings - Fork 64
Open
Labels
Description
I suggest to do AJAX calls via the wp.ajax.send( action[, options]) methods provided by wp-utils as described by Luke Woodward in his article
Using wp.ajax for Async Requests in WordPress
and talk wp.awesome! WordPress JS helpers (slides)
The main advantage being:
- By enqueuing this helper, a reference to the admin-ajax.php processor is automatically included in the page. There is no need to additionally include it anywhere through a script tag or through localizing a script.
- This method unwraps the wp_send_json_success() and wp_send_json_error() methods calling the success and error callbacks based on which wp_send_json method was used. It will supply the data sent with wp_send_json so there is no need to look for a data parameter before processing the response.
Especially the interaction with the wp_send_json_* backend functions makes life easier because it cuts the possible error scenarios in half. An AJAX request can be successful even if it failed to do it's job on the server-side. These helper functions make sure a success promise is only returned if both succeeded.
Example
wp.ajax.send( "lw_submit_comment", {
success: commentSuccess,
error: commentError,
data: {
nonce: nonce,
comment: formData
}
});Reactions are currently unavailable