Skip to content

Instantly share code, notes, and snippets.

@anka
Created April 7, 2023 18:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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