0

I am trying to check if a website is up and forward it to an error page if it's not. So far I tried doing this but it goes to error page. I checked if website was up and it is. What am I doing wrong? I am trying to send header using jQuery ajax call.

function checkpageus(){
  $.ajax({
    type: 'HEAD',
    timeout: 1000,
    url:"www.prioritymeter.net",
    statusCode:{
      400: function(response){
        window.location = "www.prioritymeter.com/static_pages/error.html";
      },
      200: function(response){
        window.location = "https://www.prioritymeter.net/wb/b/us.xhtml";
      }
    }
  });
}
<a onclick="checkpageus()" style="cursor:pointer">My Account</a>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
M.Dagiya
  • 203
  • 3
  • 13
  • 1
    Unless the website returns CORS headers in the response (which is *incredibly* unlikey for a client-facing site) then what you're trying to do will not be possible in JS due to the [Same Origin Policy](http://en.wikipedia.org/wiki/Same-origin_policy). You will have to make the request server side, using cURL for example. – Rory McCrossan Mar 14 '17 at 19:02
  • Can I make an ajax get request instead to "www.prioritymeter.net" – M.Dagiya Mar 14 '17 at 19:09
  • No. As I mentioned above, you cannot make a request to any third party domain from JS unless it explicitly contains CORS headers in the response. This is very, very unlikely unless it's an API for third party consumption, and even then some don't do it. – Rory McCrossan Mar 14 '17 at 19:12
  • If you first correct the URL you call to include the `http://` prefix, then you'll get this error in the console: `No 'Access-Control-Allow-Origin' header is present on the requested resource`, which in turn leads on to far more detail in this question: http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Rory McCrossan Mar 14 '17 at 19:14

0 Answers0