【发布时间】:2014-06-22 15:24: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', {}, {
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,我可以获取网站列表。
请告诉我,我的错误在哪里?
【问题讨论】:
标签: jquery angularjs http cors angular-resource