Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接在broswer中工作,并提供JSON,但不提供Angular_Javascript_Json_Angularjs_Service_Controllers - Fatal编程技术网

Javascript 链接在broswer中工作,并提供JSON,但不提供Angular

Javascript 链接在broswer中工作,并提供JSON,但不提供Angular,javascript,json,angularjs,service,controllers,Javascript,Json,Angularjs,Service,Controllers,我试着用Angular得到两个答案 第一,如果网站使用绿色能源,则回复 第二,这个网站的替代品是什么 app.controller('QueryController',['$http',function($http){ var site = this site.green = [] site.alternatives = [] $http.get('http://api.thegreenwebfoundation.org/greencheck/' + 'www.apple.com').succ

我试着用Angular得到两个答案

第一,如果网站使用绿色能源,则回复

第二,这个网站的替代品是什么

app.controller('QueryController',['$http',function($http){
var site = this
site.green = []
site.alternatives = []

$http.get('http://api.thegreenwebfoundation.org/greencheck/' + 'www.apple.com').success(function(data){ 
  console.log(data.result);
});

$http.get('http://www.similarsitesearch.com/api/similar/' + 'www.apple.com').success(function(data){ 
  console.log("it worked");
}); }]);
正如您所看到的,JSON提供了这两个链接。但是,当我尝试运行此操作时,会出现以下错误:

XMLHttpRequest cannot load http://www.similarsitesearch.com/api/similar/www.apple.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

我想知道正确的方法,谢谢

这看起来像是一个跨域问题。出于安全原因,浏览器将阻止对其他域的ajax请求

您可以研究这些站点是否支持CORS,或者是否可以使用JSONP获取。然而,JSONP存在一些安全问题,特别是当您与第三方打交道时


另一种选择是在您自己的域上设置一个反向代理,以发出跨域请求。

+1-几乎可以肯定是跨域问题。我对自己的教育很好奇,所以创建了以下fiddle:它试图用jQuery打开两个URL。第一个成功了,但第二个失败了,即使在浏览器地址行中输入两个都可以成功打开。我有点搞不清楚下一步该怎么做。