Initially, I have confirmed that CORS is enabled and functioning in my web API by successfully sending post/get requests from another controller. However, when CORS is disabled, I encounter issues with posting/getting data from the server. The same situation applies to obtaining a token; once CORS is disabled on the web API, I no longer receive a valid response. But currently, I am receiving a valid response.
I am attempting to retrieve a token from the web API using Angular Resource and a promise. In Fiddler, I can see status 200, indicating that I am receiving a token back. The JSON specifies "bearer token = x," which looks good. Postman also shows successful results. But when checking the debug console, I notice that the promise is throwing an error, despite Fiddler and Postman showing me a valid JSON file with a status of 200.
// Controller
.controller('LoginCtrl', ['$scope', 'Token', function ($scope, Token) {
$scope.login = function () {Token.save('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="592c2a3c2b3738343c642d3c2a2d686b6a192d3c2a2d773a3634">[email protected]</a>&password=Test123!!&grant_type=password')
.$promise.then(function (response) {
console.log(response);
});
};
}]);
In my factory:
.factory('Token', ['$resource', function ($resource) {
return $resource(baseUrl + 'token', null, {
save: {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*'
},
}
});
}]);
The response I receive in Developer Tools console is:
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","headers":{"Content-Type":"application/x-www-form-urlencoded","Accept":"/"},"data":"[email protected]&password=Test123!!&grant_type=password","url":"http://localhost:55126/token"},"statusText":""}
I am absolutely certain that I am receiving a valid JSON object from the /token call. Thank you in advance.
Update
XMLHttpRequest cannot load localhost:55126/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:9000'; is therefore not allowed access. angular.js:14642 –
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","headers":{"Content-Type":"application/x-www-form-urlencoded","Accept":"/"},"data":"username=test123@test.com&password=Test123!!&grant_type=password","url":"localhost:55126/token";},"statusText":""}
These two error lines appear consecutively.