0
<script type="text/javascript">

$(function () {
    $("#citynm1").autocomplete("http://www.mywebsite.com/getcity.php", {
        width: 160,
        autoFill: false,
        selectFirst: false
    });
});     

</script>

This script is working when site URL start with WWW but if WWW is not available this script is not working. So what can I do?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Have you verified you have mywebsite.com pointing to your web server in DNS and your web server configured to accept requests from mywebsite.com and not just www.mywebsite.com? – Dan Sherwin Jun 03 '14 at 03:43
  • Problem is with your server not script, check dns configs, virtual host configuration on server – Manjeet Sharma Jun 03 '14 at 03:48
  • 1
    @DanSherwin: could be Same Origin policy violation. Try using relative web addresses, try fetching the base URL from `location`, try changing domain... – Amadan Jun 03 '14 at 03:48
  • Try using a [root-relative path](http://stackoverflow.com/questions/5559578/having-links-relative-to-root): `"/getcity.php"`. The request use the current hostname, regardless of which it is. – Jonathan Lonowski Jun 03 '14 at 03:52

1 Answers1

0
    <script type="text/javascript">

    $(function () {

        var url="http://www.mywebsite.com/getcity.php"
         urlExists(url,function(exist){
               if(!exist){
                    url="http://mywebsite.com/getcity.php"   
               }
               urlExists(url,function(exist2){
                 if(exist2){
                   $("#citynm1").autocomplete(url,{
                       width: 160,
                       autoFill: false,
                       selectFirst: false
                     })  
                  }

               });

         }); 



    });     

    </script>

Known that :

function urlExists(url, callback){
  $.ajax({
    type: 'HEAD',
    url: url,
    success: function(){
      callback(true);
    },
    error: function() {
      callback(false);
    }
  });
}
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254