Question

On the client side using jQuery, I want to know if I can just check if a link URL is valid (i.e. doesn't return a 404). This link points to another domain, so if I just use $.get() then I end up with a permission issue. I remember reading something about using a JSONP request, but I don't remember.

Was it helpful?

Solution

I found a solution that seems to work (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 variable 'url'.

OTHER TIPS

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

In your case it won't work unless the other site is willing to cooperate or you have a proxy script on your own site.

If you want your script to work with sites not under your control, your best bet will be to use a proxy or a iframe hack.

You can't make a request like that to another domain. That is a security feature in the browser. You may have to try doing something in an iframe or something and checking that.

jsonp wont do you much...

What you should do is create a local proxy easily on your server using your favorite language that'll simply load the url you pass to it and return the response code. Then, use jquery ajax to load up the proxy page with the url you want to test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top