繁体   English   中英

如何设置Cors以使AngularJS访问外部API?

[英]How to setup cors for angularjs to access external api?

getjson.controller('controller', function($scope, $http){
  var url = "http://api.8coupons.com/v1/getsubcategory"; 

  $http.get(url).success(function(data, status, headers, config){
    $scope.jsondata = data;
    console.log(data);
  }).error(function(data, status, headers, config){});
});

(编者注:在外部函数体上添加了一个右括号。)

此代码给我以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.api.8coupons.com/v1/getsubcategory. This can be fixed by moving the resource to the same domain or enabling CORS.

有人可以告诉我什么地方出问题了,我该如何解决?

我找到了答案。

var url = "http://api.8coupons.com/v1/getcategory?callback=JSON_CALLBACK";

也许可以帮助某人。

如果遇到这种错误,请将类型从get / post更改为jsonp

然后在网址中附加此参数

 "?callback=JSON_CALLBACK" 

无引号

暂无
暂无

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

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