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

Links work in broswer and provide JSON but not with Angular

本文关键字:Angular 但在 JSON broswer 工作 链接      更新时间:2023-09-26

我正在尝试使用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存在一些安全问题,尤其是当您与第三方打交道时。

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

相关文章: