连接到与角工厂API端点 [英] Connect to API endpoint with Angular factory
问题描述
我想通过最简单的AngularJS片段与API端点连接,但具有被阻止我的一些问题。
I am trying to connect with API endpoint via the simplest AngularJS snippet but have some problems which are stopping me.
下面是我的控制器:
app.controller('appController', ['$scope', 'skills',
function($scope, skills) {
skills.success(function(data) {
$scope.skillsList = data;
});
}
]);
和我厂:
app.factory('skills', ['$http', function($http) {
return $http.get('https://www.persevy.com/skills')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
}]);
我甚至prepared一个 Plunkr 这也没有成功,请告诉我,在上述code中的问题。
I have even prepared a Plunkr for this but also without success, please tell me where is the problem in the above code.
推荐答案
您的问题似乎与 CORS :基本上,你不能,如果它不允许在服务器端访问通过Ajax的域。这是最现代的浏览器中的安全功能。使用命令行你就不会遇到这样的问题,如卷曲
或邮差
Chrome的推广。
Your issue seems related to CORS : basically, you cannot access a domain via Ajax if it's not allowed on the server side. This is a "security" feature on most modern browser. You won't encounter this problem using command line such as curl
or Postman
chrome's extention.
如果您拥有域名 https://www.persevy.com/ ,确保域名请求该数据被允许在访问控制允许来源
头,以及作为HTTP动词( GET
, POST
, PUT
...或 *
为每个HTTP方法)。
If you own the domain https://www.persevy.com/, make sure the domain requesting the data is allowed in the Access-Control-Allow-Origin
header, as well as the http verb (GET
, POST
, PUT
... or *
for every http methods).
如果您使用的是API,你应该找到有关的事项的文档中的东西。
If you are using an API, you should find something in the documentation regarding that matters.
修改
我在回报率有点生疏,但是从我的谷歌搜索,貌似可以使用 rails- CORS 宝石,或者尝试这一主旨。
I'm a bit rusty in RoR, but from my googling, looks like you can use the rails-cors gem, or try this gist.
基本上,它归结为以下两个头添加到服务器的响应:
Basically, it comes down to add the two following headers to the server's response :
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
这篇关于连接到与角工厂API端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!