AngularJS:通过https提供$resource,Angular从https切换到http
AngularJS: Serving $resource via https, Angular switches from https to http
我在AngularJS项目中设置了一个ng-resources,它从RESTneneneba API获取数据。在开发和测试环境中一切都很好(两者都通过http运行)。但是,在生产中,REST请求由于No 'Access-Control-Allow-Origin' header
错误而失败。
整个页面都是通过https提供的,因此我希望Angular的请求也会通过https。我用一个相对路径(例如/api/)设置了资源,然而AngularJS似乎从https变成了http。
我的问题
- 是什么原因导致请求http而不是https,即使有相对的api url
- 关于如何处理
$http
或resource
的https请求,有什么好的Angular文档吗 - 这可能是nginx的服务器配置问题吗?对我来说,Angular似乎只是自动从https切换到http
完整错误消息
XMLHttpRequest cannot load http://www.example.com/api/data/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.example.com' is therefore not allowed access.
AngularJS资源设置
.factory('Data', ['$resource',
function($resource){
return $resource('/api/data''/', {}, {
query: {
url:'/api/data/',
method: 'GET',
isArray: true}
},
{ stripTrailingSlashes: false });
}])
我在AngularJS项目中列出了http和https域的白名单,但这并没有对https请求做出任何更改。
.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'self',
'http://www.example.com/**',
'https://www.example.com/**'
]);
})
我认为它与https无关,您可以使用带有$http和$resource的https而不做任何更改。如果仅使用https访问索引,则所有请求都将是https。
问题应该是关于"No‘Access Control Allow Origin’header"。您应该在服务器端启用CORS。已经有很多关于这方面的指南了。
$sceDelegateProvider不适用于https。仅支持http。
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 使用angular重定向到html页面
- angular.js没有'无法在PhoneGap中处理视图标记
- Javascript(Angular)从一个对象数组到第二个数组查找值
- angular 1.5应用程序中的导航栏
- angular的下拉菜单
- Angular只从数组中获取所需的数据
- 如何将不可变的js导入angular 2(alpha)
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将JSON对象传递给angular指令
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Angular templateUrl 在 https 上被阻止 “此请求已被阻止;内容必须通过HTTPS提供
- AngularJS:通过https提供$resource,Angular从https切换到http
- Angular 2: XMLHttpRequest无法加载https://imgur-apiv3.p.mashape.c
- Angular的ngmodel只在firefox/https中不更新$scope
- Angular的视图和html可以使用HTTP,而JSON的RestAPI调用可以使用HTTPS吗?