0

I have a link say, https://www.dotnerpearls.net (which is wrong since it is an HTTP site by default). I need to validate this URL using an ajax call, so that if it throws an error I can show a warning that "the user should not prefix an http with an https".

tried the following,

var k = "http://www.wikipedia.org"
$.ajax({
    type: 'GET',
    url: k,
    success: function () {
        alert("success");
    },
    error: function () {
        alert("failure");
    }
});

But this always goes to error part irrespective of the url given. What am i missing ?? Pretty new to ajax, so guess its some basic thing I am missing.

the following CODE WORKED eventually:

 var testUrl = "https://www.dotnetpearls.net";
          $.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;
                  }
              }
          });

It was the cross domain causing the error.

wickjon
  • 900
  • 5
  • 14
  • 40
  • 4
    Have you allowed for cross domain requests? – Gareth James Dec 20 '13 at 13:22
  • 2
    AJAX will not work for a cross-domain request unless the server has been specifically set up to accept those requests. HTTPS://whatever.com and HTTP://whatever.com are not compatible. I think it's called a same-origin policy or something. – MonkeyZeus Dec 20 '13 at 13:23
  • 4
    Post it to a PHP file of your own (on the same domain) and check via CURL what header is being send back. – putvande Dec 20 '13 at 13:24
  • Here is some info for you http://stackoverflow.com/questions/1105934/ajax-using-https-on-an-http-page – MonkeyZeus Dec 20 '13 at 13:24
  • @Targarian I am assuming that you have a script on your server that validates the URL for you. Otherwise you will definitely have to look at cross-domain policies. – Talha Masood Dec 20 '13 at 13:25
  • Something to consider is that if you are using sessions and a variable was set while using HTTPS then an AJAX call to HTTP will have no access to that session... – MonkeyZeus Dec 20 '13 at 13:26
  • I've attempted to answer a question like this before, but it's not really that easy to do with JavaScript without the help of some Serverside code for reasons described in the comments above. http://stackoverflow.com/questions/19933679/jquery-to-validate-url-from-another-domain-not-working/19934169#19934169 – MackieeE Dec 20 '13 at 13:29
  • @TalhaMasood: Nope, cant do it in that way. Sometimes my users enter a url without prefix. – wickjon Dec 20 '13 at 13:30
  • 1
    do it server side...AJAX is far too limited for this – charlietfl Dec 20 '13 at 13:32
  • 1
    @Targarian The only thing I find logical here is that you write the URL validation script using some server side language and pass the URL to this script using ajax. If you follow this architecture, things will go smoothly for you. AJAX has lot of limitations that will give your audience a poor experience. – Talha Masood Dec 20 '13 at 13:33
  • 1
    And +1 to all those comments which raised the issue of cross-domain :) – Talha Masood Dec 20 '13 at 13:35
  • @Targarian one thing more. Always try to debug your code before posting in here. You can use FireBug to debug your JavaScript code. Then see for error messages and try to Google against those. Always put in some extra effort :) – Talha Masood Dec 20 '13 at 13:38

0 Answers0