Use jQuery to check if the URL of another domain is 404 or not?

On the client side using jQuery, I want to know if I can check if the link URL is valid (i.e. does not return 404). This link points to a different domain, so if I just use $ .get (), then I get a permission problem. I remember reading something about using a JSONP request, but I don’t remember.

+1
jquery cross-site
source share
4 answers

I found a solution that works (using YQL):

$.getJSON("http://query.yahooapis.com/v1/public/yql?"+ "q=select%20*%20from%20html%20where%20url%3D%22"+ encodeURIComponent(url)+ "%22&format=xml'&callback=?", function(data){ if(data.results[0]){ // do whatever } } ); 

Assumes the URL you want to check is in the 'url' variable.

+4
source share

JSONP works if the server you are calling to can return a JSONP response. This basically means a script that calls the callback function on your page after loading. see http://en.wikipedia.org/wiki/JSON#JSONP

In your case, this will not work if another site does not want to cooperate or you have a proxy script on your own site.

If you want your script to work with sites that are not under your control, it is best to use a proxy server or iframe hacker.

+1
source share

You cannot make such a request in another domain. This is a security feature in the browser. You may have to try to do something in an iframe or something else and check it out.

0
source share

jsonp will not be much to you ...

What you need to do is create a local proxy server on your server using your favorite language, which will simply load the URL that you pass to it and return the response code. Then use jquery ajax to load the proxy page with the URL you want to check.

0
source share

All Articles