【问题标题】:How to setup cors for angularjs to access external api?如何为 angularjs 设置 cors 以访问外部 api?
【发布时间】:2014-11-13 07:15:46
【问题描述】:
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.

谁能告诉我哪里出了问题,我该如何解决这个问题?

【问题讨论】:

标签: angularjs angularjs-scope angularjs-ng-repeat angular-ui angular-ui-router


【解决方案1】:

我找到了答案。

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

也许它可以帮助某人。

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

然后在url中追加这个参数

 "?callback=JSON_CALLBACK" 

不带引号

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2012-12-13
  • 2021-04-17
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多