1

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;
});
MoLow
  • 3,056
  • 2
  • 21
  • 41
Mohd Waseem
  • 119
  • 6

1 Answers1

1

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)

MoLow
  • 3,056
  • 2
  • 21
  • 41
  • After adding your line it show "A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true"Now using the below code. (function () {'use strict';angular.module('app').config(['$httpProvider', function ($httpProvider) {$httpProvider.defaults.withCredentials = true;$httpProvider.defaults.crossDomain = true;}]).service('UtilityService', ['$log', '$q', '$http', function ($log, $q, $http) {this.getHTTPGET= function(url){return $http({method: 'GET',url: url}).then (function(data){return data;}).catch(function(data){return data;});};}]);})(); – Mohd Waseem Aug 30 '16 at 17:01
  • 1
    you need to change the `Access-Control-Allow-Origin` header in server side, view http://stackoverflow.com/questions/19743396/cors-cannot-use-wildcard-in-access-control-allow-origin-when-credentials-flag-i – MoLow Aug 30 '16 at 17:10