0

I am trying to rewrite a working $.ajax server call to use AngularJS $http.put. The $http version returns 401 unauthorised.

The ajax call I am rewriting looks like this:

$.ajax({
	url: domain + "/api/v1/user/logout/",
	timeout: 10000,
	type: "POST",
	contentType: "application/json",
	beforeSend: function(xhr) {xhr.setRequestHeader("Authorization", api_user())},
	success: function(data) {
		if (data.success) {
			notify("Thanks for signing out");
							
		}
	}
});

The AngularJS equivalent I have written looks like this:

logoutUser: function() {
   var config = {headers:  {
        'Authorization': api_user()
      }
   };
   return $http.post(apiDomain + '/api/v1/user/logout/', config).then(function(response)    {
       return response;
   });
}

When this is run the server returns a 401 unauthorised and the 'then' part of the $http.post is not entered.

EDIT: More information

Using firebug it says the request headers are:

POST /api/v1/user/logout/ HTTP/1.1

Host: localhost:8000

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0

Accept: application/json, text/plain, */*

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Content-Type: application/json;charset=utf-8

Referer: http://localhost:8100/

Content-Length: 40

Origin: http://localhost:8100

X-Forwarded-For: 12.13.14.15

Connection: keep-alive

Pragma: no-cache

Cache-Control: no-cache

The POST source, according to Firebug is:

{"headers":{"Authorization":"ApiKey bill:bfab6e5a1fb6e4e405756dcf14a634e86ba05c7b"}}

Does this mean the authorisation is being send as data not as a header?

0

3 Answers 3

1

You have to add error function for handling 401 response.

Check below sample code for adding error callback function

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think he is asking how to handle a 401 response but why is he getting a 401.
When this is run the server returns a 401 unauthorised and the 'then' part of the $http.post is not entered. I think this is the question
0

Depending on the API/server end point you are hitting they do not all authorize calls from the front-end. Steam community market (https://developer.valvesoftware.com/wiki/Steam_Web_API/Feedback) is an example that I have found that angular front-end calls simply are not authorized. I had to do the http calls from the back-end and serve up to the front.

2 Comments

The $.ajax call is working when run from another app (one that does not use AngularJS). So I am guessing the problem must be at the client rather than server end?
I have added some more information about the problem from Firebug
0

The second parameter is the data you want to send. The config is the third one.

$http.post(apiDomain + '/api/v1/user/logout/', {}, config)

5 Comments

Tried that but it didn't make any difference. Still got 401.
@BillNoble Did you check if api_user() returns something and the header gets actually sent? FWIW: You can omit the parentheses (and single quotes) and write Authorization: api_user. Angular will then call the function itself.
api_user() is returning what I expect.
I have added some more information about the problem from Firebug
@BillNoble According to the Firebug info the code has not been updated as I suggested. Or maybe it has and you need to clear the cache, restart the server etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.