0
$scope.fetch =function()
                        {
                        $http(req)
                                .success(function(data,status,headers,config){
                                        if(status==0)
                                        {
                                            console.log(data);
                                            console.log(status);

                                        else{
                                .error(function(response,status,headers,config) {

                console.log("error");
                console.log(headers);
                console.log(fd);
            });

What i mean is if status is 0 i would like to print that status is zero else it comes to the error block and prints a alert msg.I tried like above but unable to correct the syntax.

3 Answers 3

2

Yes, it's possible to have if/else blocks in JavaScript. You do, however, need to close any open brackets you create and maintain the structure of any functions you write. Let's start with the HTTP call:

$http(req)
  .success(function(data,status,headers,config) {
    // do this on success
  })
  .error(function(response,status,headers,config) {
    // do this on error
  });

What you were trying to do was mix an if/else block with the structure of these functions. That, of course, won't work. The JavaScript interpreter would have no idea how to make sense of that. But within any given function, you can certainly have an if/else block.

For example, if within a successful operation you may receive different response content and wish to handle it differently, you'd do that entirely within the success callback:

$http(req)
  .success(function(data,status,headers,config) {
    if (status == 0) {
      // do one thing
    } else {
      // do another thing
    }
  })
  .error(function(response,status,headers,config) {
    // do this on error
  });
Sign up to request clarification or add additional context in comments.

2 Comments

I'm having a pretty hard time finding what status code 0 actually means and how it will enter the success block.
@skubski: Completely agreed. I really only used it as a demonstration because it was the conditional check made in the original question. Whatever condition the code needs to check would be fine. I've only ever seen a 0 in cases where I was debugging and shut down the server application mid-request, leaving the browser with no meaningful response.
0

I think its not possible, because the success and error callbacks are called by angular by considering the status of the $http request, if the status is 200 it will call the success callback, otherwise it will call the error callback.

$http(req)
    .success(function (data, status, headers, config) {
        // this will call when there is no error on `$http` request.
    }).error(function (response, status, headers, config) {
        // this will call when there is error on `$http` request.
    });

Comments

0

You can do this by put an external method for error. This method will be call on your error and on your succes if status is 0.

$scope.error = function(response,status,headers,config){
     console.log("error");
     console.log(headers);
     console.log(fd);
}
$scope.fetch =function(){
      $http(req).success(function(data,status,headers,config){
                     if(status==0){
                          console.log(data);
                          console.log(status);
                     }else{
                          $scope.error(data,status,headers,config);
                     }
                 .error($scope.error(response,status,headers,config));

Comments

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.