0

I'm trying to check if url exist using JQuery and following

Checking a Url in Jquery/Javascript

However I am getting

XMLHttpRequest cannot load http://10.16.20.22:8080/xxx. No 'Access-Control-Allow- Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

My question is how can I check if url exist?

Community
  • 1
  • 1
user829174
  • 6,132
  • 24
  • 75
  • 125
  • What is the question ? – Thom-x Jul 23 '15 at 12:46
  • My question is how can I check if url exist? – user829174 Jul 23 '15 at 12:47
  • Have you the control of `http://10.16.20.22:8080/xxx` ? – Thom-x Jul 23 '15 at 12:48
  • You can try to create iframe than check, is there any content – Slawa Eremin Jul 23 '15 at 12:50
  • 1
    possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – W van Rij Jul 23 '15 at 12:51
  • 2
    If you don't have control of the remote system, one solution would be to setup a "proxy" script on your own webserver (using the server side language of your choice). This proxy will check the existence of the remote system and report the result to your current Javascript (which in turn will not check the remote system but the proxy ... with the remote URL as a parameter) – devnull69 Jul 23 '15 at 12:51

1 Answers1

1

Here dataType: jsonp for cross-domain request, that means request to different domain where dataType: json for same domain-same origin request.

function urlExists(url, successCallBack) {
  $.ajax({
    url: url,
    dataType: "jsonp",
    statusCode: {
      200: function(response) {
        console.log('status 200');
        successCallBack();
      },
      404: function(response) {
        console.log('status  404 ');
      }
    }
  });
}
urlExists("http://google.com", function() {
  console.log('side exists so I got executed');
});

Codepen link http://codepen.io/anon/pen/waEVmv

Shiva
  • 11,485
  • 2
  • 67
  • 84