Javascript 角度,使用jsonp绕过';允许访问源';

Javascript 角度,使用jsonp绕过';允许访问源';,javascript,json,angularjs,cors,Javascript,Json,Angularjs,Cors,正在尝试使用外部API $http({method: 'json', url: url }) .then(function(data){ console.log(data); }, function(err, code){ console.log(err); }) 我得到的错误是: Response to preflight request doesn't pass access control check: No 'Acce

正在尝试使用外部API

$http({method: 'json', url: url })
      .then(function(data){
        console.log(data);
      }, function(err, code){
        console.log(err);
      })
我得到的错误是:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.
我无法将API配置为从不同的域接受。相反,我尝试使用
jsonp

$http({method: 'jsonp', url: url })
如果我检查chrome开发工具中的网络选项卡,我可以看到一个正确接收的200 OK响应和数据

但angular指出:
意外标记:


这是因为angular需要“json回调”。我是否可以从客户端修复此问题?

您需要使用回调的名称作为“JSON\U回调”

请参考下面的代码-

angular.module('alertApp',['alertApp.controllers','alertApp.services']);
angular.module('alertApp.services',[]).factory('alertAPIservice',函数($http){
var-alertAPI={};
alertAPI.getAlerts=函数(){
返回$http.jsonp('https://angularjs.org/greet.php?name=StackOverflow&callback=JSON_CALLBACK');
//在url中使用&callback=JSON_callback
}
返回警报API;
});
angular.module('alertApp.Controller',[])
.controller('mainController',函数($scope,alertAPIservice){
$scope.message='Hello Mid World!';
$scope.alertsList=“加载数据”;
alertAPIservice.getAlerts().then(函数(响应){
$scope.alertsList=response.data;
},函数(错误,a,b){
$scope.alertsList=错误;
});  
}); 

{{message}}
{{alertsList | json}}

没有。不能使用浏览器来修复它,你需要一个服务器,在某处(不一定是你自己的)来解释JSON并将其转换成JSONP。