加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS CORS不会发送带有凭据的所有cookie,但jQuery会发送

发布时间:2020-12-17 17:48:13 所属栏目:安全 来源:网络整理
导读:我在我的前端使用AngularJS v1.2.16.我创建了一个简单的服务: app.provider('Site',function () { var apiServer = 'http://api.example.com'; this.$get = ['$resource',function ($resource) { var site = $resource(apiServer + '/sites/:action:id',{},
我在我的前端使用AngularJS v1.2.16.我创建了一个简单的服务:

app.provider('Site',function () {

  var apiServer = 'http://api.example.com';

  this.$get = ['$resource',function ($resource) {
    var site = $resource(apiServer + '/sites/:action:id',{},{
      query: {
        withCredentials: true
      },checkDomain: {
        method: 'POST',withCredentials: true,params: {
          action: 'checkDomain'
        }
      },save: {
        method: 'PUT',withCredentials: true
      }
    });

    return site;
  }];
});

当我尝试获取控制器中的网站列表时:

app.controller('SitesCtrl',['$scope','Site',function ($scope,Site) {
   $scope.sites = Site.query();
}]);

Angular不会发送带有请求的cookie,我无法获取网站列表.

但是当我通过jQuery发送相同的请求时:

$.ajax({
  type: 'GET',url: 'http://api.example.com/sites',xhrFields: {
    withCredentials: true
  },success: function (data) {
    console.log(data);
  }
});

随请求发送的Cookie,我可以获取网站列表.

请告诉我,我的错误在哪里?

解决方法

由于$resource服务是$http的工厂,您是否尝试在应用配置中将$CreProvider的withCredentials默认值设置为true?

.config(function ($httpProvider) {
  $httpProvider.defaults.withCredentials = true;
}

参考Angular docs,你的语法似乎对于withCredentials是正确的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读