user3231840
user3231840

Reputation: 25

Abort JSONP request AngularJs

I have the following code:

    var canceler = $q.defer();
    $http.jsonp(urlWithParams, {
        timeout : canceler.promise
    }).error(function(data, status, headers, config) {
        ...
    }).success(function(data) {
        ...
        }
    });
    canceler.resolve();

The error handler of the request gets executed however in the network log of both Firefox and Chrome give 200 responses and return the JSON response. So while the application behaves like the request was aborted, in reality it wasn't? When canceling the request I would have expected the request to have been aborted than returning a 200 success.

Upvotes: 2

Views: 532

Answers (2)

chubbsondubs
chubbsondubs

Reputation: 38676

The final statement in your code canceler.resolve() will trigger the error. From the angularjs docs:

timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.

So I think if you don't call canceler.resolve() it won't call the error function.

Upvotes: 0

Pauli Price
Pauli Price

Reputation: 4237

You're not doing anything wrong.

Generally, because http is stateless, you can't abort a request once it's been sent to the server. You can, however, stop waiting for and ultimately ignore an eventual response - as you are doing here.

Unless you feel that the error handler shouldn't have been fired because the response succeeded? You don't say whether you're concerned that it was incorrectly failed, or that the aborted request received a response.

Upvotes: 1

Related Questions