Sto usando AngularJS v1.2.16 nel mio frontend. Ho creato un semplice servizio:AngularJS CORS non invia tutti i cookie con credenziali, ma jQuery invia
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;
}];
});
Quando provo a ottenere l'elenco dei siti in mio controller:
app.controller('SitesCtrl', ['$scope', 'Site', function ($scope, Site) {
$scope.sites = Site.query();
}]);
angolare non invia i cookie alla richiesta, e non riesco a ottenere l'elenco di siti.
Ma quando invio stessa richiesta per jQuery:
$.ajax({
type: 'GET',
url: 'http://api.example.com/sites',
xhrFields: {
withCredentials: true
},
success: function (data) {
console.log(data);
}
});
cookie inviati con richiesta e posso ottenere l'elenco dei siti.
Dimmi, per favore, dov'è il mio errore?
Questa risposta l'ha risolto per me! Non riuscivo a farlo funzionare a meno che non fosse un valore predefinito. – Tillman32