How to enable withCredentials to flag on XMLHttpRequest in AngularJS for “Access-Control-Allow-Origin”

Ahamed Fazil Buhari
 
Senior Developer
January 9, 2018
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
2759

Hi everyone,

In this article we will see how to enable withCredentials config in you Angular project. Before going further let us see the requirement of using withCredentials, in the following link you can find the description of withCredential – https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Requests_with_credentials

 angular.module("App", [])
     .controller("Ctrl", function ($scope, $http) {
 
         var URL = "https://mywebapi/api/names";
         var requestHeader = {
             getHeader: {
                 'headers': {
                     'accept': 'application/json;odata=verbose'
                 }
             }
         };
 
         var result = $http.get(URL, requestHeader.getHeader)
             .then(function (response) {
                 var data = JSON.parse(response.data);
                 $scope.values = data
                 console.log(data);
                 return data;
             });
     })
     .config(['$httpProvider', function ($httpProvider) {
         $httpProvider.defaults.withCredentials = true;
     }])
 

Happy Coding

Ahamed

Author Info

Ahamed Fazil Buhari
 
Senior Developer
 
Rate this article
[Total: 0    Average: 0/5]
 
Ahamed is a Senior Developer and he has very good experience in the field of Microsoft Technologies, especially SharePoint 2013, 2016 and O365, Azure, ASP.NET, SQL Server, IIS and client ...read more
 

Leave a comment