1
function registrationSave(data){
            return $http({
                method: 'POST',
                url: 'registration/save',
                data: data
            }).then(function(response){
                 console.log("@@@1 "+JSON.stringify(response));
            },function(error){
                  console.log("@@@2"+JSON.stringify(error));
                  return error.data;
            }).catch(function (data) {
                // Handle error here
            });
    };

THis is the server response for the service in case of error

//Server response

{
    "code": 400,
    "message": "Failled with registration Id 1,341.",
    "result": "False"
}

In case of 400 error client code response is undefined ? It never going to error section in the client code.

Why is it so ?

This is the interceptor used

   /**
         * To handle error response.
         */
        function responseError(error) {
              if (error.status === 401 || error.status === 403) {
                  if(error.data != null && error.data.code === 401 ){ //access denied error
                      toaster.error( 'Access denied', 'User does not have privilege');
                  }
              }

        }

In case of 400, Can I return back to the method here ?

8
  • you have not passed proper data, may be you missed id or you passed some wrong data. so that it is showing 400 bad request Commented Dec 6, 2016 at 6:28
  • Bad request I'm sending from server side, but client is receiving as undefined Commented Dec 6, 2016 at 6:32
  • 1
    Do you have any interceptors in your application? Commented Dec 6, 2016 at 6:33
  • I think u r not sending the error as an error. Rather you are just sending JSON response. The problem is not with AngularJS, its the server-side code that needs to be fixed. Commented Dec 6, 2016 at 6:34
  • @Raghu Venmarathoor Yes Its going to Interceptor. Commented Dec 6, 2016 at 6:56

1 Answer 1

1

If the HTTP status code is between 200 and 299 the success function of $http is called, otherwise the error function is called. It appears that the HTTP status code returned from the server is a 200, while the code field in the message body is 400. In the success function there is no return so undefined is what you see. You can verify if this is happening if you observe @@@1 in the browser console. In the browser's developer tools network tab you can also see the HTTP status code and verify if it is between 200 and 299.

Sign up to request clarification or add additional context in comments.

7 Comments

Status Code:400 , it is 400. DO I need to change this ?
When the error will be called, which status code range ?
In the browser's console, do you see @@@1? If you add console.log('@@@3') into the catch function, you should see either @@@1, @@@2, or @@@3.
@@@1 undefined, I can see this. It should go to error section with the value why it give as @@@1 undefined, that was my question.
My answer is to your question about why it doesn't go to the error section.
|

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.