I am building a JavaScript application to retrieve STOCK information from Google Finance API.
finance.google.com/finance/info?q=nasdaq:AAPL
If I copy paste the link in the browser then I receive the JSON reply correctly
// [ { "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" } ]
I tried Yahoo finance url as well. Same issue for that too. This was my url
This is my JS Code.
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);
}
PS: Instead of var request even if I add a direct http request string just for the sake of testing the code, still responseText is empty.
xhr.open('GET', "http://ipinfo.io/json", true);
Not sure what is going wrong. Also in Chrome I get readyState as 1 and status as 0, in Internet Explorer I get readystae as 4 and status as 200.*
curl
, instead of client-side js and see if the response is different. P.S. isn't this API deprecated? googlecode.blogspot.com/2011/05/… – James Pan//
which will comment out all the JSON. I think is a kind of protection Google done because Finance has no API (developers.google.com/finance/?hl=es). – yuriy636XMLHttpRequest cannot load https://finance.google.com/finance/info?q=nasdaq:AAPL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access.
I would recommend to find another API stackoverflow.com/questions/10040954/… – yuriy636