I need Cors in angularjs, I am not using cors for webapi ,because i am using the api of others, i am only trying to get data with basic authentication using angularjs , i had tried using POSTMAN it is working fine but using my angularjs application it is not working i had tried a lot but it is showing error as below:
Failed to load resource: the server responded with a status of 401 (Unauthorized) index.html:1 XMLHttpRequest cannot load https://api.url/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63122' is therefore not allowed access. The response had HTTP status code 401.
I had tried as sample code as below:
var myApp = angular.module("myApp", ['ngCookies']);
myApp.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]);
myApp.controller("GetDataController", ["$scope", "$http", function ($scope, $http) {
$scope.GetToken = function () {
debugger;
var appdata = $.param({
grant_type: 'credentials',
scope: 'Customer',
});
$http({
async: true,
crossDomain: true,
method: 'GET',
url: 'https://api.url.net/token',
data: appdata,
headers: {
"username": "847fd9f9fg9h7h66jl8ljdggff",
"password": "302kfsg7fhhjh0dgjngfdjd",
"Authorization": "Basic Mufdnfaffa8439flg8g"
}
}).then(function (response) {
debugger;
$scope.displaymessage = response.data;
}, function errorCallback(response) {
$scope.displaymessage = response;
console.log(response)
});
};
}])
where i should have change as it is working in POSTMAN, why it is not working in my application..