$http.post() should execute error function but is executing success function.

3,274 views
Skip to first unread message

Praym

unread,
Jan 12, 2013, 3:39:42 PM1/12/13
to ang...@googlegroups.com
Has anybody come across this issue: I am making a $http.post() call like this in my application

$http.post(apiurl,JSONData).
 success(function(r,s){ 
  console.log('success'); 
 }).
 error(function(r,s){ 
  console.log('error');
 }); 

I know my 'apiurl' is returning 401 error, but $http.post() is displaying success message. 
Any clue why this is happening? Is this is bug? 

I tried with jQuery $.ajax() call and that returned 'error' message.
Here is what is working (jQuery),

  var request = $.ajax({
            url: apiRoot+'/login',
            type: "POST",
            data: JSON.stringify({
                'username':$scope.data.username,
                'password': (!$scope.data.password)? '': Sha1.hash($scope.data.password)
                }),
            dataType: "json"
        })

        request.done(function(data){
           console.log('Success', data);
        });

        request.fail(function(jqXHR, textStatus){
            console.log('Fail', textStatus);
        });

And here is my exact problem code that is displaying buggy behaviour:

 $http.post(apiRoot+'/login', JSON.stringify({
                'username':$scope.data.username,
                'password': (!$scope.data.password)? '': Sha1.hash($scope.data.password)
            }))
            .success(function(responseData, status){
                console.log('Login successful, set session cookies and redirect', responseData, status);
                $scope.api_key = $cookies.hrt_session;
                console.log('API KEY:', $scope.api_key);
                $location.path('/mypage');

            })
            .error(function(responseData, status){
                console.log('error:', responseData, status);
            });


The server is returning a 401 status, but I am still getting 'Login successful' message.

Thanks for any help.

Praym

Peter Bacon Darwin

unread,
Jan 12, 2013, 3:49:03 PM1/12/13
to ang...@googlegroups.com
Difficult to tell without a running example.  Can you see the response in the browser window?



Praym

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
 
 

Joshua Miller

unread,
Jan 12, 2013, 3:49:14 PM1/12/13
to angular
Hello!

Can you provide the request and response information you get from Chrome Developer Tools or Firebug? A plunker would be even better. It should work fine: http://plnkr.co/edit/ILDjRC6m1eyxGeONkoKz?p=preview

Josh



Praym

Praym

unread,
Jan 12, 2013, 4:14:13 PM1/12/13
to ang...@googlegroups.com
Hi this is what i see in the Dev tool:

  1. Request URL:
  2. Request Method:
    POST
  3. Status Code:
    401 Unauthorized
  4. Request Headersview source
    1. Accept:
      application/json, text/plain, */*
    2. Accept-Charset:
      ISO-8859-1,utf-8;q=0.7,*;q=0.3
    3. Accept-Encoding:
      gzip,deflate,sdch
    4. Accept-Language:
      en-US,en;q=0.8
    5. Connection:
      keep-alive
    6. Content-Length:
      29
    7. Content-Type:
      application/json;charset=UTF-8
    8. Cookie:
      hrt_session=1358025803%7CJchkLa5YNDVzbGDfQXFQK2ev%2B%2Fj%2B06DB7gSiWKBrGek%3D%7C212f541be355bb7924e75f72ad0da2f05d4e9716
    9. Host:
      my
    10. Origin:
    11. Referer:
    12. User-Agent:
      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17
    13. X-Requested-With:
      XMLHttpRequest
  5. Request Payload
    1. {"username":"","password":""}
  6. Response Headersview source
    1. Cache-Control:
      max-age=0,no-cache,no-store
    2. charset:
      utf-8
    3. Connection:
      Keep-Alive
    4. Content-Length:
      71
    5. Content-Type:
      application/json
    6. Date:
      Sat, 12 Jan 2013 21:11:01 GMT
    7. Expires:
      Mon, 17 Dec 2012 01:00:00 GMT
    8. Keep-Alive:
      timeout=5, max=100
    9. Last-Modified:
      Sat, 12 Jan 2013 21:11:02 GMT
    10. Server:
      Apache/2.2.19 (Win32) PHP/5.3.5
    11. Set-Cookie:
      hrt_session=1358026261%7CxuANUxukME2wBM6qR6uKzzkfyf8O66ey2j6fEtXrEDg%3D%7Ca804b006f6400243de6a573dd6a716a0baa979c5; path=/; expires=Sat, 12-Jan-2013 21:31:01 UTC
    12. X-Powered-By:
      PHP/5.3.5



I am not sure how will I replicate the problem in plunkr as my api is sitting on this dev machine.

Thanks

Praym

unread,
Jan 12, 2013, 4:18:31 PM1/12/13
to ang...@googlegroups.com
And this is the console log :

  1. POST http://my/dev/v2/src/api-v2/public/login 401 (Unauthorized) angular.js:9002
    1. (anonymous function)angular.js:9002
    2. LoginController.$scope.doLogincontrollers.js:310
    3. (anonymous function)angular.js:6166
    4. (anonymous function)angular.js:12563
    5. Scope.$evalangular.js:7808
    6. Scope.$applyangular.js:7888
    7. (anonymous function)angular.js:12562
    8. v.event.dispatchjquery-1.8.3.min.js:2
SSuccess:: Object {error: "Sufficient data not provided, username or password missing."} 401 controllers.js:317

Peter Bacon Darwin

unread,
Jan 12, 2013, 4:45:43 PM1/12/13
to ang...@googlegroups.com
Can you put a breakpoint on this line in your angular,js file, https://github.com/angular/angular.js/blob/master/src/ng/http.js#L118, and debug through the resolution?
Pete

Praym

unread,
Jan 12, 2013, 5:01:31 PM1/12/13
to ang...@googlegroups.com
It paused on that breakpoint.

Here are the local variables at that breakpoint
  1. status401
  2. thisundefined

Praym

Peter Bacon Darwin

unread,
Jan 12, 2013, 5:07:31 PM1/12/13
to ang...@googlegroups.com
And so what happens next?  Does it call deferred.resolve() or deferred.reject()?

Praym

unread,
Jan 12, 2013, 5:07:58 PM1/12/13
to ang...@googlegroups.com
I have stepped through further but cant see the clue. Its going in success section of the code all the time, where as it should be going towards error parts.
Could there be anything on the server side that is causing this behaviour.  I changed the error stated to 403, 500 from the server, but its the same 'success' message each time.

Praym

Praym

unread,
Jan 12, 2013, 5:22:33 PM1/12/13
to ang...@googlegroups.com
Its calling deferred.resolve() 


I cant make out. This is what I am seeing. 

Thanks
Praym
Message has been deleted

Jean-Dominic Laroche

unread,
Feb 5, 2013, 5:11:50 PM2/5/13
to ang...@googlegroups.com
I am facing a similar issue with a 

$resource.save({},{}, function() 
{
 console.log('success');
}, function() 
{
console.log('error');
});

Even if I receive a http status = 500, i see success in the console. 

Jean-Dominic Laroche

unread,
Feb 6, 2013, 8:52:11 AM2/6/13
to ang...@googlegroups.com
It was my mistake. I had an http interceptor that was catching the error first and returning the response instead of rejecting it...
Reply all
Reply to author
Forward
0 new messages