I've recently started to learn JavaScript, my learning process was going smoothly until I got to JSON, when I started trying to get a JSON out of a URL I got stuck real bad, I've tried loads of methods I found here, and loads of different URL's and nothing seems to work. Well cutting to the chase, this is what I've got:
- I am using IIS to create a local server, and I'm running my script from there;
- I created a simple JSON file on JSONbin.io;
- I'm trying to access it using the code I got from JSONbin's website;
- I created some console.logs for debugging;
- I found out that although the console doesn't point out any error, the request function never runs;
- MYKEY is the secret key i got from JSONbin.io
If anyone could shed a light on this matter, or simple providing a simple code that would work for me, it would help a lot.
<p> test </p>
<script>
let req = new XMLHttpRequest();
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
console.log("1");
console.log(req.responseText.name);
}
};
req.open("POST", "http://api.jsonbin.io/b/5ec04a83a47fdd6af1645b86", true);
console.log("2");
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("secret-key", "MYKEY");
console.log("3");
</script>