Mohd Waseem
Mohd Waseem

Reputation: 119

Send cookies in ajax angular

I want to send the document cookie in the http call in angular. I have already used withCredentials but it can only send the browser level cookie. I also use the cross domain ,but it is not working fine. Below is the code which I am using.

$http({
    method: 'GET',
    url: url,
    xhrFields: { withCredentials: true },
    crossDomain: true,    
    header:{ 'SHOPSESSIONID' : sessionStorage.getItem("SHOPSESSIONID") }
}).success(function(data){
    return data;
}).error(function(data){
   return data;
});

Upvotes: 1

Views: 790

Answers (1)

MoLow
MoLow

Reputation: 3084

use $httpProvider in config phase:

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
}])

and you could give up the xhrFields and crossDomain proporties

btw, angular uses then instead of success, and catch instead of error. (that is the promise syntax)

Upvotes: 1

Related Questions