Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// function.php wp_enqueue_scripts
wp_localize_script( 'main-js', 'rootUrl', array(
'siteUrl' => get_site_url()
) );
// main.js
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', rootUrl.siteUrl + '/wp-json/wp/v2/posts');
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
console.log(data);
// createHtml(data);
} else {
console.log("We connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
// function createHtml(postData) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.