Firefox addon-sdk: handle http request timeout

I am creating a firefox add-in using add-s sdk. I need to make an HTTP request to a specific page, and I want to process the connection timeout, but could not find anything in the api: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk /request.html

What I'm really looking for is a callback if the client cannot connect to the server.

Is there any way to achieve this?

+4
source share
1 answer

An SDK request will always invoke onComplete when the request is considered complete for the network. This means that onComplete is invoked anyway, ignoring if the request returned an error or success.

To find out what your error is, you need to check the Response object (the object passed to the onComplete function) the "status" property ( response.status ). It contains the status code for the request. To view status codes, view the list on the mozilla dev network . If the response status is 0, the request has completed completely and the user is probably disconnected, or the goal cannot be reached.

The timeout will be either a status code 504 or 0. The implementation will be similar to the implementation:

 var Request = require("sdk/request"); Request({ url: "http://foo.bar/request.target", onComplete: function(response) { if(response.status==0||response.status==504) { // do connection timeout handling } // probably check for other status codes else { // assume the request went well } } }).get(); 

I personally use the check function on the request object, which returns me a number, which depends on whether I have the correct answer, an error from the web server or a connection problem (status codes 4xx and 0).

+9
source

All Articles