AngularJS CORS don';t使用凭据发送所有cookie,但jQuery发送
我在前端使用AngularJSV1.2.16。我创建了一个简单的服务:AngularJS CORS don';t使用凭据发送所有cookie,但jQuery发送,jquery,angularjs,http,cors,angular-resource,Jquery,Angularjs,Http,Cors,Angular Resource,我在前端使用AngularJSV1.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: {
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);
}
});
随请求发送Cookies,我可以获得站点列表
请告诉我,我的错误在哪里?由于
$resource
服务是$http
的工厂,您是否尝试在应用程序配置中将$httpProvider
的with credentials
默认值设置为true
.config(function ($httpProvider) {
$httpProvider.defaults.withCredentials = true;
}
关于角度,您的语法对于
with credentials
似乎是正确的,这个答案为我解决了这个问题!我无法让它工作,除非它是默认的。