M
M
Mikhail Beschetnov2014-08-13 15:37:48
Angular
Mikhail Beschetnov, 2014-08-13 15:37:48

How to properly recognize $http error in AngularJS?

There was a following problem.
Angularjs uses $http.post to load data from the server. In the case when the data source (its own API) is not available for some reason (well, or there is no Internet), $http returns status = 0 in the error handler. Which is fine. Having received this status, I want to inform the user about problems in the network or API. However, the same status is assigned to the request in the case when there is simply a transition to another page with an interruption, respectively, of the current ajax request (there is such bad luck). And, of course, in this case, there is no need to issue any frightening messages to the user.
The question is whether it is possible to separate flies from cutlets, i.e. to recognize the interruption of the request by the user and the real unavailability of the remote source?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TekVanDo, 2014-08-14
@TekVanDo

Most browsers have events offline, online - https://developer.mozilla.org/en-US/docs/Web/API/N... you can track their status.

S
Sergey Romanov, 2014-08-14
@Serhioromano

Use the error method and status.

$http.post(...).
      error(function(data, status) {
          if(status == 404) {
               alert('Not found');
          }
      });

I'm not sure that the status will be 404. Check the log through the console what is returned there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question