2

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I do want the error handler be triggered however I wouldn't have expected the 200 response when looking at the request. When previously aborting ajax requests with jQuery the request status is changed to canceled rather than returning a response.
jQuery also needs a response for status. You may have a bug somewhere else. Do you have callback=JSON_CALLBACK in url?
Indeed, the callback exists in the url which gets coverted to &callback=angular.callbacks._0 for example.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.