开发者

use javascript to test if server is reachable

I have a javascript application that loads data asynchronously from a server within my company's firewal开发者_StackOverflow社区l. We sometimes need to show this application outside of the firewall but it is not always possible to get on the VPN. For this reason, we configured the firewall to allow public requests for this data on a specific port. The catch: when I am inside the corporate network, I can only load data using the server's DNS name due to a peculiar configuration of the firewall. Outside of the corp network, I have to use the server's public IP address..

Currently we distribute two versions of the javascript application -- one which works within the firewall and one that works outside of it. We would like to distribute one app -- one that tests the URLs and then, depending on which is reachable, continue to use that address to load data.

I have been using jQuery's $.ajax() call to load the data and I noticed there is a timeout parameter. I thought I could use a short timeout to determine which server is unreachable.. However, this "countdown" doesn't seem to start until the initial connection to the server is made.

Any thoughts on how to determine, in javascript, which of two servers is reachable?


Use the error event:

$.ajax({
    url: dnsUrl,
    success: ... // Normal operation
    error: function () {
        $.ajax({
            url: ipUrl,
            success: ... // Normal operation
        });
    }
});


You may put some dummy-images on the server and try to load them. the onload-event of the image that was successfully loaded should fire.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜