Kenneth Lynne
Kenneth Lynne

Reputation: 15579

Is there a way to know if a $http request timed out?

It seems the status code is 404 for both timed out requests and for Not found.

What is the best way to check if the request failed because of a spotty connection (and eventually retry the request) in angular?

Upvotes: 1

Views: 2481

Answers (1)

Emerson Farrugia
Emerson Farrugia

Reputation: 11353

I don't get a 404 for timeouts.

Just ran this code

$http({ method: "GET", url: "http://localhost:4789/foo", timeout: 5000})
    .then(
    function () {
        console.log("all OK");
    },
    function (response) {
        console.log(response);
    });

against a URL that busy waits. After 5 seconds, the failure callback of the $http promise fires with response:

Object {data: null, status: 0, headers: function, config: Object}

The status is 0, not 404. Chrome's Network tab shows the request as "(cancelled)".

Upvotes: 1

Related Questions