我正在尝试使用 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 存在一些安全问题,尤其是在与第三方打交道时。
另一种选择是在您自己的域上设置反向代理来发出跨域请求。
关于javascript - 链接可以在浏览器中使用并提供 JSON,但不能在 Angular 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26154561/