I am writing Javascript to know the different sites(dev, staging and prod) are UP or not. below is my code.
function URLStatusCheck(url)
{
try {
$.ajax(url,
{
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
crossDomain: true,
statusCode: {
404: function (xhr) {
alert("error 404");
},
200: function (xhr) {
alert("Site is UP");
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error");
}
}
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("Fail");
});
} catch (err) { alert(url); }
}
I have problem with invalid URLs, below error are showing in the browser mand for the invalid URLs.
Failed to load resource: the server responded with a status of 404 (Not Found)
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
and those errors I am not able to catch them in any where (try/catch, error or 404). Could some one help me where(method or level) I can handled those errors.
Thanks, KPK
I am writing Javascript to know the different sites(dev, staging and prod) are UP or not. below is my code.
function URLStatusCheck(url)
{
try {
$.ajax(url,
{
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
crossDomain: true,
statusCode: {
404: function (xhr) {
alert("error 404");
},
200: function (xhr) {
alert("Site is UP");
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error");
}
}
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("Fail");
});
} catch (err) { alert(url); }
}
I have problem with invalid URLs, below error are showing in the browser mand for the invalid URLs.
Failed to load resource: the server responded with a status of 404 (Not Found)
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
and those errors I am not able to catch them in any where (try/catch, error or 404). Could some one help me where(method or level) I can handled those errors.
Thanks, KPK
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 8, 2014 at 10:17 Pavan Kumar kotaPavan Kumar kota 451 gold badge1 silver badge9 bronze badges 02 Answers
Reset to default 3Your syntax is incorrect, and you probably miss a reference to jQuery, but anyway this code should be the remended way of error handling in newer JQuery > 1.8:
function URLStatusCheck(url) {
$.ajax(url, {
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
crossDomain: true
})
.done(function (data, textStatus, jqXHR) {
alert("success");
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert("error");
})
.always(function () {
//alert("plete");
});
}
However, have in mind that you are potentially going to do a cross-origin request (unless the source and destination sites you want to check are on the same domain), and depending on your server configuration the request could be blocked. So you will have to enable CORS on the servers you want to ping from elsewhere.
http://enable-cors/server.html
There is an extra closing bracket "}" for ajax method options
function URLStatusCheck(url) {
try {
$.ajax(url,
{
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
crossDomain: true,
statusCode: {
404: function (xhr) {
alert("error 404");
},
200: function (xhr) {
alert("Site is UP");
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error");
}
//} -- the extra one
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("Fail");
});
} catch (err) { alert(url); }
}