简体   繁体   中英

angualrjs how to ignore status 400 after POST success

Is there a way to ignore or bypass this error code 400 and just trigger a function when this error occurs?

I tried this : --

vm.createOrder = function () {
 var deferred = $q.defer(); // update
 orderService.createOrder()
    .then(function (response, status) {
       deferred.resolve(response); // update
       if(status == 400) {
          console.log('Should never happen but trigger a function anyway! ');
          localStorage.clear();
        }
        console.log('Successfully created an order: ', response.status);

    })
}

orderService: --

createOrder: function () {
            return $http({
                url:  'apiURL',
                method: 'POST'
            })
        }

but it doesn't even console.log the string in the if(status) condition or in the success , but the POST method does go through so its posting the data I want to post but it returns an error code of 400 .

Edit fixed

orderService.createOrder()
                .then(function (response) {
                    console.log('Successfully created an order: ', response.status);
                })
                .catch(function (e) {
                    console.log('Should never happen but trigger a function anyway! ');
                    localStorage.clear();
                    console.log(e);
                })

Your service should look like this.

createOrder: function () {
               return $http.post('SomeURL')
                     .then(function (response) {
                          return response.data;
                     },
                       function (response) {
                           return $q.reject(response);
                     });
                }

You should call it like this, rather than a try catch.

orderService.createOrder().then(function(response){
   //Successfull Call
},function(response){
  //Error on call
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM