0
function getData(){

    $.get("http://www.google.com/finance/getprices?q=.NSEI&x=NSE&i=60&p=1d&f=d, o, h, l, c, v", function(data){
        alert("Data:"+data);
});

 }   
getData();

The above URL gives response in string format and not in JSON format, so I can't use $.getJSON.

I am able to get the data in Java, but using JavaScript, the data is not coming and the alert is not executed.

How can I get data from external URL through $.get method?

Rajat
  • 37
  • 11
  • 1
    Read up on same origin policy. Also, the variable should be `data`, not `Data`. – Etheryte Sep 06 '14 at 16:20
  • I am able to get data in jason format through other google finance URL-https://finance.google.com/finance/info?client=ig&q=NSE:RELIANCE – Rajat Sep 06 '14 at 16:23

2 Answers2

0

You are trying to access data from a different domain than it is hosted on, which is in violation of the Same-Origin Policy. In essence, this is a security measure taken to prevent cross-site scripting attacks.

Therefore, you cannot make requests to a different domain unless it supports CORS. If you look in your browser console, you should see this error:

XMLHttpRequest cannot load http://www.google.com/finance/getprices?q=.NSEI&x=NSE&i=60&p=1d&f=d,%20o,%20h,%20l,%20c,%20v. Origin http://example.com is not allowed by Access-Control-Allow-Origin.

There are ways around it, but that is most certainly the reason why you can access the data in Java and not in JavaScript.

Community
  • 1
  • 1
AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • I am able to get data in jason format through other google finance URL-finance.google.com/finance/info?client=ig&q=NSE:RELIANCE – – Rajat Sep 06 '14 at 16:25
  • 1
    @user3367964 Yes, but you can't make a request to it, because it also does not support CORS. – AstroCB Sep 06 '14 at 16:33
0

Try making the request from the server rather than the front end, for example, if you are doing this on a node app or a java app, create an endpoint on your server and then expose it as a rest service, then call that endpoint from the UI using $.get() or xml http request.

So, basically the server will then be fetching that data and passing it on the front-end when you call that endpoint on your server.