Created
March 10, 2019 17:01
-
-
Save AbmSourav/7e6c446fa5f0e0b68ccf74cd53ecad57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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