-1
AJAXはCORSの要求になるため、動作しません。 Image source trickを使ってやってみましたが、失敗しました(更新されたコード)。
JSFiddleは
var sites = {
google: {
url: "http://www.google.com",
id: "googleStatus"
},
dummy: {
url: "http://www.dummysitealphabeta2.com",
id: "dummySiteStatus"
}
}
pingSite(sites.google); // Should update status to "Up"
pingSite(sites.dummy); // Should update status to "Down"
// ping site using image trick wiz. Add source of site to image
// and add handler to image onload or onerror
function pingSite(site) {
var image = new Image();
image.src = site.url;
image.onload = function() {
console.log("loading loaded for " + site.url + "successfully");
}
image.onerror = function() {
console.log("Image loading failed for " + site.url);
}
}
// Using AJAX - Won't work because of CORS
function getStatus(site) {
$.ajax(site.url).done(function() {
// Http 200 OK
$(site.id).html("Up");
}).fail(function() {
// HTTP 401,
$(site.id).html("Down");
});
}
<table>
<tr>
<td>Google</td>
<td id="googleStatus"></td>
</tr>
<tr>
<td>Dummy Site</td>
<td id="dummySiteStatus"></td>
</tr>
</table>