Custom Ajax & Rest API (Pro)
About the feature
Example of Rest API usage
script.js
async function makeRequest(postId, value) {
// 1. todo use '/wp-json/advanced_views/v1/view/' for Views,
// and put your View ID at the end
// 2. todo use '/wp-json/advanced_views/v1/card/' for Cards,
// and put your Card ID at the end
await fetch('/wp-json/advanced_views/v1/{view|card}/{6630e2da1953e}', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
// todo your arguments here
'postId': postId,
'value': 'value,
}),
}).then(response => response.json())
.then((response) => {
// todo process response, the variable already contains the parsed object
console.log('Request complete', response);
})
.catch(error => () => {
console.error('Request error:', error)
});
}view.php
card.php
Request Authentication
1. Create and pass nonce to the template (in view.php or card.php)
2. Define nonce as a JS window variable (in Twig or Blade template)
3. Use nonce in the request headers (script.js)
Example of Ajax usage
script.js
view.php
card.php
Request Authentication
Last updated