How do I check if a certain host (other than my own server) is reachable from client's machine? For example, I would check if https://www.facebook.com
is reachable. If it is reachable then only I would place a facebook widget on the page. Solutions I could find on the web relies on loading some resource from the host server. But I just want to ping the server if it is reachable. I wouldn't know what resource to try loading.
Asked
Active
Viewed 1,392 times
1

Dilip Raj Baral
- 3,060
- 6
- 34
- 62
-
I would strongly suggest you don't do this in JS as you'll most likely be blocked by the Same Origin Policy. Unless the receiving domain has explicitly allowed third party requests in JS (which is extremely rare) then the request will always fail – Rory McCrossan Jan 19 '17 at 10:24
-
@RoryMcCrossan So, what do you suggest? – Dilip Raj Baral Jan 19 '17 at 10:32
-
Make the AJAX to your local server containing the domain you want to look up, then make the server do the cross domain request. – Rory McCrossan Jan 19 '17 at 10:32
-
@RoryMcCrossan But I want to check if the particular host is reachable from the client's machine. The client could be in some network where access to certain hosts might be blocked. – Dilip Raj Baral Jan 19 '17 at 10:35
-
Sorry, that's not possible reliably in browser-based JS. – Rory McCrossan Jan 19 '17 at 10:36
1 Answers
0
Use this:
$.ajax({url: "https://www.facebook.com",
type: "HEAD",
timeout:1000,
statusCode: {
200: function (response) {
alert('Working!');
},
400: function (response) {
alert('Not working!');
},
0: function (response) {
alert('Not working!');
}
}
});
It will give you an appropriate answer for its reachability status.

Sorangwala Abbasali
- 821
- 10
- 26
-
1Please include jQuery reference in your snippet - If you are inserting a snippet, that is supposed to work. You can select jQuery from the left menu – Developer Jan 19 '17 at 10:22
-
2Did you actually try this? You'll be blocked by the SOP: `No 'Access-Control-Allow-Origin' header is present on the requested resource` https://jsfiddle.net/vh2Lessh/ – Rory McCrossan Jan 19 '17 at 10:23
-
2