我在我的前端使用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;
}];
});
Run Code Online (Sandbox Code Playgroud)
当我尝试获取控制器中的网站列表时:
app.controller('SitesCtrl', ['$scope', 'Site', function ($scope, Site) {
$scope.sites = Site.query();
}]);
Run Code Online (Sandbox Code Playgroud)
Angular不会发送带有请求的cookie,我无法获取网站列表.
但是当我通过jQuery发送相同的请求时:
$.ajax({
type: 'GET',
url: 'http://api.example.com/sites',
xhrFields: {
withCredentials: true
},
success: function (data) …Run Code Online (Sandbox Code Playgroud)