0

I am kind of new to JQuery. My requirement is to validate a link from another domain. If success, then redirect (Open the page a new window) to that link, otherwise show alert.

What I tried in Jsfiddle are given below:

$.ajax({ 
    url: "/user/login", 
    method: 'head', 
    error: function(){ 
        alert('Failure'); 
    }, 
    success: function(){ 
        alert("Success"); 
    } 
})

The above one succesfully validated the URL. But once I changed the url to http://www.google.com, it is not working. Code snippet is given below:

$.ajax({ 
    url: "http://google.com", 
    method: 'head', 
    error: function(){ 
        alert('Failure'); 
    }, 
    success: function(){ 
        alert("Success"); 
    } 
})

Any idea why this is not working and is there any way to solve that? I just found out that cross domain validation is not supported in JQuery. Is it true?

Sudipta Deb
  • 1,040
  • 3
  • 23
  • 43
  • So I can't validate an url which is not in my same domain with JQuery? – Sudipta Deb Nov 12 '13 at 15:53
  • You can't make AJAX requests to external domains due to the same-origin policy. However, there is a great answer here that shows some ways to circumvent it: http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Guilherme Sehn Nov 12 '13 at 15:54
  • @SudiptaDeb How would you 'validate' the link on the domain anyway? – MackieeE Nov 12 '13 at 15:57
  • Exactly I would like to validate the link based on the fact whether there is a page available for that link. If yes then do redirect otherwise alert('Failure') – Sudipta Deb Nov 12 '13 at 15:58
  • @SudiptaDeb So if it's simply a page that retrieves an 200 GET header, does that count as valid? (Could be a redirected 404) because even if a page did have content, how would you distinguish a legitimate page from a bad one? Secondly, would a **Ping** be sufficient enough to know that the page on the domain sits on is live? – MackieeE Nov 12 '13 at 16:01
  • @MackieeE: I think 200 GET Header is perfect in my situation. If 200 GET Header is fine, then I will consider that page as a valid page and do the redirect. – Sudipta Deb Nov 12 '13 at 16:05

1 Answers1

1
<script>
    var testUrl = "http://www.google1.com";
    $.ajax({ 
        url: testUrl, 
        dataType: 'jsonp',
        crossDomain: true,
        timeout: 5000,
        complete: function( e, xhr, settings ) {    
            switch( e.status ) {
               case 200:
                   window.open( testUrl );
               break;
               default:
                   alert( 'Not Valid' );
                   return false;
               break;
            }
        }   
    });
</script>

Alternatively, I would send a AJAX request to an internal Server Side Script would then check using some server side methods, such as cURL/get_file_contents() for PHP.

MackieeE
  • 11,751
  • 4
  • 39
  • 56
  • First of all, thanks for your quick reply. I tried your code with http://www.google.com. window.open(..) is working fine. But when I am changing the url to http://www.google1.com. I am expecting alert message should come us. But that is not happening? – Sudipta Deb Nov 12 '13 at 16:21
  • @SudiptaDeb Tweaked it a little, had to set a Timeout because the google1.com wasn't returning a 404, thus Failing. Also adjusted to .complete() as this is always triggered. – MackieeE Nov 12 '13 at 17:00
  • Now the problem is coming with IE9. With IE9, with invalid url, alert('') is not coming. Any idea why? – Sudipta Deb Nov 13 '13 at 09:19
  • Add `async: true` to your options (Despite it being default), just in case, for some reason when it's false, even the `timeout` fails to run. – MackieeE Nov 13 '13 at 09:50
  • No luck with async: true. :-( – Sudipta Deb Nov 13 '13 at 12:12
  • @SudiptaDeb Having trouble re-creating the same problem, I lowered the `timeout` number to about 2000 in case. – MackieeE Nov 14 '13 at 09:34