user829174
user829174

Reputation: 6362

JQuery check if url exist No 'Access-Control-Allow-Origin'

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?

Upvotes: 0

Views: 1578

Answers (1)

Shiva
Shiva

Reputation: 12514

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

Upvotes: 1

Related Questions