Skip to content

Instantly share code, notes, and snippets.

@anka
Created April 7, 2023 18:17
Show Gist options
  • Save anka/22bb66db00d8816a273a5f63407475ee to your computer and use it in GitHub Desktop.
Save anka/22bb66db00d8816a273a5f63407475ee to your computer and use it in GitHub Desktop.
Example JavaScript code to consume a custom JSON API in a Wordpress plugin
document.addEventListener('DOMContentLoaded', async () => {
const data = {
key1: 'data1',
key2: 'data2',
};
const response = await fetch('/wp-json/custom-api-route/v1/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const result = await response.json();
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment