2014-05-06 7 views
5

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?

risposta

8

Dal momento che il servizio $resource è una fabbrica di $http hanno si tenta di impostare il valore di default withCredentials al vero per il vostro $httpProvider nella vostra applicazione di configurazione?

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

Riferendosi al angolare docs, la sintassi sembra corretta per il withCredentials.

+0

Questa risposta l'ha risolto per me! Non riuscivo a farlo funzionare a meno che non fosse un valore predefinito. – Tillman32