-
-
Save jevgen/7b1ffc0d7409021c695ab7007a4326f3 to your computer and use it in GitHub Desktop.
Using wp.apiRequest to access custom endpoints.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // See: wp-includes/js/api-request.js | |
| // Returns a jqXHR object. See: https://api.jquery.com/jQuery.ajax/#jqXHR | |
| wp.apiRequest({path: '/namespace/vendor/v1/config'}) | |
| .then(configOptions => console.log(configOptions)); | |
| // jqXHR object has method then(), but does not have methods catch() or | |
| // finally(). Use fail() or always() instead | |
| wp.apiRequest({path: '/namespace/vendor/v1/config'}) | |
| .done(configOptions => console.log(configOptions)) | |
| .fail((request, statusText) => { | |
| if (request.responseJSON && request.responseJSON.message) { | |
| console.error(request.responseJSON.message); | |
| } else { | |
| console.error(statusText); | |
| } | |
| }) | |
| always(() => console.log('Finish.')); | |
| // Pass data to the request | |
| wp.apiRequest({ | |
| path: '/namespace/vendor/v1/entity', | |
| data: { | |
| id: 17 | |
| } | |
| }) | |
| .then(...); | |
| // By default the type of the request is "GET" | |
| wp.apiRequest({ | |
| path: '/namespace/vendor/v1/entity/delete', | |
| type: 'POST', | |
| data: { | |
| id: 17 | |
| } | |
| }) | |
| .then(...); | |
| /* | |
| * See also: | |
| * https://api.jquery.com/jQuery.ajax/#data-types | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment