-1

I need to check if a URL (www.google.com / www.googlesss.com)exists. we support various browsers (IE, Chrome, Firefox etc...) so the solution needs to be able to support all of those.

I.E

var backendUrl = "https://www.google.co.in";
$.ajax({
   type: "POST",
   url: backendUrl
}).done(function (result) {
   console.log("working");
}).fail(function () {
   alert("Sorry URL is not access able");
});

Output:

if backendUrl = "https://www.google.co.in"; then return working if backendUrl = "https://www.yahoo.co.in"; then return working if backendUrl = "https://www.googlessss.co.in"; then Sorry URL is not access able

Jignesh Hirpara
  • 135
  • 1
  • 5

2 Answers2

1

You can make HEAD request first to identify resource is able to comminicate with you without sending response data.

So you can detect is this url is valid with minimal amount of transfered data.

$.ajax({
    type: 'HEAD',
    async: true,
    url: url,
}).then(function(message,text,jqXHR){
    //valid url
}, function() {
    //not valid url
});

https://developer.mozilla.org/ru/docs/Web/HTTP/Methods/HEAD

VadimB
  • 5,533
  • 2
  • 34
  • 48
0

You can do:

var backendUrl = "https://www.google.co.in";
$.get(backendUrl).done(function () {
  // REACHABLE
}).fail(function () {
  // UNREACHABLE
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Nimrod Shory
  • 2,475
  • 2
  • 19
  • 25