Reputation: 1163
I have a Admin panel(I am creating) with a list of URLs. I would like create script which can check to see if the urls is currently working or goes to a 404 page. Is this possible?
Upvotes: 2
Views: 9670
Reputation: 1536
Ajax calls to Get/Head will work only if the URL to check belongs to same site. If you are trying to check for the URL which belongs to some other site, then you will get Cross-site reference error.
In that case , you need check this at server side. Create an ajax call to server side method. At server side , create HttpRequest for the url and check its HttpResponse and reply back to ajax call.
Upvotes: 4
Reputation: 296
You can do this:
<a href="/somelink" id="test1">Link1</a> <span id="result1"></span>
$.ajax($("#test1").attr("href"), {
statusCode: {
404: function() {
$("#result1").html("not working");
},
200: function() {
$("#result1").html("working");
}
}
});
Upvotes: 4
Reputation: 148714
try this : (assuming the page are in the same domain)
$.ajax(
{
type: "get",
url: 'page.aspx',
cache: false,
statusCode: {
404: function ()
{
alert('page not found');
}
},
async: true
});
Upvotes: 3