Chuck Aguilar Chuck Aguilar - 2 years ago 803
AngularJS Question

getting status -1 instead 401 Angularjs

I'm trying to get a response from server. The function looks so:

function getOverview() {
var req = {
method: 'GET',
url: base,
headers: {
'authorization': 'Bearer ' + GottenTokens.getSessionToken()

return $http(req).then(
function successCallback(response) {
return response;
}, function errorCallback(response) {
return response;

When the status is 200, there's no problem, it returns the good response with status 200. But... when the status is 401, than it returns a response with status -1:

Object {data: null, status: -1, config: Object, statusText: ""}

I was trying it in postman and I saw there's the status 401 and also in browser it says so:

XMLHttpRequest cannot load http://localhost:5000/overview. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.

It means that the browser and postman take the status from server, but it doesn't come in Angularjs. I was trying this post: Capture Http 401 but it doesn't help me. I have that function in a "factory".

By the way, the server is written in akka. I don't know if it has something to do. Before was it in NodeJS and I didn't have problems. Since it's in akka I can't get the status in Angularjs.

Could anybody help me?

Answer Source

When doing cross-site requests, the server has to add to ALL responses a valid CORS header. This includes error responses like 401.

Make sure you can see the "Access-Control-Allow-Origin" header in your 401 response, it's most likely missing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download