1

I have this code

var body="ok";
var suc=0; var failed=0;
$http({
           url: API.toUrl('api/xxxx/xxxx'),
                method: 'POST',
                data: body
            }).then(function(response) {
                if(response.status==200){
                    suc=suc+1;
                }
                if(response.status==400){
                    failed=failed+1;
                }                        
            });

My problem in this is that I can't obtain the 400 Status Code, I am only getting 200 but not 400. How can I get 400 status code in my response parameter.

I am working in Angular, any idea for obtain the 400?

Thanks

Irfan Anwar
  • 1,878
  • 17
  • 30
ZizouJd
  • 79
  • 3
  • 12

4 Answers4

0

400 status code will come as an error, then accept two functions as parameters first one for OK response and second one for errors so you have to catch 400 on error function.

So if you want to catch it you should do it like this:

  var body = "ok";
  var suc = 0;
  var failed = 0;
  $http({
      url: API.toUrl('api/xxxx/xxxx'),
      method: 'POST',
      data: body
  }).then(
      function(response) {
         if (response.status == 200) {
          suc = suc + 1;
         }
      }, 
      function(error) {
          //Catch 400 here
      }
  );
pachonjcl
  • 723
  • 4
  • 11
0

If you are using PHP server to write your api's/services then use below lines to manually send 400 to your $http request

<?php

// Get the current response code and set a new one
var_dump(http_response_code(400));

// Get the new response code
var_dump(http_response_code());
?>

[UPDATE] You can see more examples here to check how to send response codes: PHP: How to send HTTP response code?

Community
  • 1
  • 1
Irfan Anwar
  • 1,878
  • 17
  • 30
0

You need to use another function for errors (400):

var body="ok";
var suc=0; var failed=0;
$http({
           url: API.toUrl('api/xxxx/xxxx'),
                method: 'POST',
                data: body
            }).then(function(response) {
                alert response.status ;
              },function error(response) {
                alert response.status ;
            });
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

From the Docs:

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called.

-- AngularJS $http Service API Reference

var body="ok";
var suc=0; var failed=0;
$http.post(url, data).then(function onSuccess(response) {
    if(response.status==200){
        suc=suc+1;
    };
    //return to chain response
    return response;
}).catch(function onReject(errorResponse) {
    if(errorResponse.status==400){
        failed=failed+1;
    }
    //throw to chain rejection
    throw errorResponse;                        
});

If chaining from the httpPromise, be sure to use a throw statement in the onReject handler to avoid converting the rejection to a success.

georgeawg
  • 48,608
  • 13
  • 72
  • 95