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();

上面的 URL 以字符串格式而不是 JSON 格式给出响应,所以我不能使用 $.getJSON。

我能够在 Java 中获取数据,但使用 JavaScript,数据不会出现,警报也不会执行。

如何通过$.get方法从外部 URL 获取数据?

4

2 回答 2

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.

于 2014-09-06T16:23:30.990 回答
0

尝试从服务器而不是前端发出请求,例如,如果您在节点应用程序或 java 应用程序上执行此操作,请在您的服务器上创建一个端点,然后将其公开为一个休息服务,然后从UI 使用$.get()xml http请求。

因此,基本上,当您在服务器上调用该端点时,服务器将获取该数据并将其传递到前端。

于 2017-09-27T17:57:49.303 回答