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?

6
  • So I can't validate an url which is not in my same domain with JQuery? Commented Nov 12, 2013 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: stackoverflow.com/questions/3076414/… Commented Nov 12, 2013 at 15:54
  • @SudiptaDeb How would you 'validate' the link on the domain anyway? Commented Nov 12, 2013 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') Commented Nov 12, 2013 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? Commented Nov 12, 2013 at 16:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

First of all, thanks for your quick reply. I tried your code with google.com. window.open(..) is working fine. But when I am changing the url to google1.com. I am expecting alert message should come us. But that is not happening?
@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.
Now the problem is coming with IE9. With IE9, with invalid url, alert('') is not coming. Any idea why?
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.
No luck with async: true. :-(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.