简体   繁体   English

在JavaScript中从外部来源获取json

[英]getting json from external source in javascript

Json issues with javascript and jquery. javascript和jQuery的Json问题。 Trying to load some JSON using javascript. 尝试使用JavaScript加载一些JSON。

I have it working using: 我有使用它的工作:

See it here: http://jsfiddle.net/5pjha/789/ 在这里查看: http : //jsfiddle.net/5pjha/789/

var url = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";

$.getJSON(url, function (json) {
    alert(JSON.stringify(json.results));
});

But it dosnt work on the following urls, why is this? 但是它不能在以下网址上工作,为什么呢?

https://poloniex.com/public?command=return24hVolume https://poloniex.com/public?command=return24hVolume

https://bittrex.com/api/v1/public/getmarkets https://bittrex.com/api/v1/public/getmarkets

https://api.mintpal.com/v1/market/summary/ https://api.mintpal.com/v1/market/summary/

Are the following urls not correct JSON ? 以下网址是否是不正确的JSON?

Thanks 谢谢

The google's api set the Access-Control-Allow-Origin header to * , so you could access it by cross domain. 谷歌的api将Access-Control-Allow-Origin标头设置为* ,因此您可以跨域访问它。

While other urls you provided do not, so you will got an error like below: 虽然您提供的其他网址没有,所以您将得到如下错误:

XMLHttpRequest cannot load https://api.mintpal.com/v1/market/summary/ . XMLHttpRequest无法加载https://api.mintpal.com/v1/market/summary/ No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://fiddle.jshell.net ' is therefore not allowed access. 因此,不允许访问源“ http://fiddle.jshell.net ”。

I believe the issue is down to whether or not the servers you are requesting your JSON from have cross-origin resource sharing ( CORS ) enabled. 我认为问题归结于您从中请求JSON的服务器是否启用了跨域资源共享( CORS )。

you can see in the headers from the google service that they set Access-Control-Allow-Origin:* This is not the case for teh other URL's you list. 您会在Google服务的标头中看到它们设置了Access-Control-Allow-Origin:*您列出的其他URL并非如此。

To get around this you will need some form of proxy so that you can request from a server either on the same domain or a server that enables CORS. 要解决此问题,您将需要某种形式的代理,以便可以从同一域中的服务器或启用CORS的服务器中请求。

For ajax request to any server, You need to define Access-Control-Allow-Origin header for client. 对于对任何服务器的ajax请求,您需要为客户端定义Access-Control-Allow-Origin标头。 which is absent in given. 在给定的情况下不存在。 You need to define origin of XMLHttp request in server who can request. 您需要在可以请求的服务器中定义XMLHttp请求的来源。

For more info refer this link 有关更多信息,请参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM