Skip to content

Instantly share code, notes, and snippets.

@AbmSourav
Created March 10, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbmSourav/7e6c446fa5f0e0b68ccf74cd53ecad57 to your computer and use it in GitHub Desktop.
Save AbmSourav/7e6c446fa5f0e0b68ccf74cd53ecad57 to your computer and use it in GitHub Desktop.
// 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