I am currently working on a JavaScript project that involves fetching STOCK data from the Google Finance API.
When I manually paste the link into my browser, I can successfully retrieve the JSON response:
// [ { "id": "22144" ,"t" : "AAPL" ,"e" : "NASDAQ" ,"l" : "108.51" ,"l_fix" : "108.51" ,"l_cur" : "108.51" ,"s": "0" ,"ltt":"10:48AM EDT" ,"lt" : "Aug 11, 10:48AM EDT" ,"lt_dts" : "2016-08-11T10:48:42Z" ,"c" : "+0.51" ,"c_fix" : "0.51" ,"cp" : "0.47" ,"cp_fix" : "0.47" ,"ccol" : "chg" ,"pcls_fix" : "108" } ]
But when I tried using the Yahoo Finance URL, I faced the same issue. Here is the URL I used:
Yahoo Finance URL
This is the JavaScript code snippet I am working with:
var url = "http://finance.google.com/finance/info?q=nasdaq:";
function getJSONReply() {
var url_req = url.concat(arguments[0]);
alert(url_req);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText.length);
}
}
xhr.open('GET', url_req, true);
xhr.setRequestHeader('Access-Control-Allow-Headers', '*');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET');
xhr.addEventListener("load", reqListener);
xhr.send();
}
function reqListener() {
var sub1 = this.responseText.substring(5,this.responseText.length);
var sub2 = sub1.substring(0, sub1.length - 2);
parse_JSON(sub2);
}
Note: Even when testing with a direct HTTP request string instead of using `var request`, the responseText remains empty.
xhr.open('GET', "http://ipinfo.io/json", true);
I'm unsure about what's causing the issue. In Chrome, the readyState is 1 and the status is 0, while in Internet Explorer, the readyState is 4 and the status is 200.*