Here is an example of how to add an Ajax Autocomplete Cities List to any website without generating any cross-site scripting errors, and without the need for any server side code or proxies. Fortunately there is not a lot that you need to know to get this running, but if you are interested in knowing how it works and why it does not raise site cross-scripting errors, then checkout the links below the following sample code. This script requires no server side code (php or otherwise) as it uses one of Geobytes’ free web services to get the list of cities. OK, first here is a demo:
And here is the fully working example code sample (Please click on the link and it will dynamically insert the sample code inline here. It is hidden by default to save room – so that you can see the effect selecting a city above has on the Cities Details table below.). Click here to open and close the code sample inline
Geobytes Get City Details JSON-P API
For a comprehensive description of each of the above fields please see the Understanding the Geo Tags & Geobytes Headers page.Once again, click here to open and close the code sample inline
|Region Code||City Code||CityId||Latitude||Capital City||Nationality
If you need assistance implementing this, or if you have any suggestions or improvements then please feel free to post them in the page comments below.
Do I need an API Key?
Not if you are using it as it is intended to be used – and that is being accessed from the browser – where your requests will be spread over a large number of different IP Addresses. Alternatively, if you were calling it from a server, then our server would see a lot of requests originating from the same IP Address and would identify your server’s requests as a denial of service attack after about 4,000 requests in the same 60 minute period. I can’t see why any one would want to access this API from a server, but if you do expect to be performing more than 4,000 requests per hour from the same IP Address, then you should consider becoming a $9.99 VIP Member to gain VIP access and appending your account’s access token to your requests, or registering your site’s referral URL with us, so that your excess requests do not get blocked by our DOS mitigation system.
OK, So how does it work, and why doesn’t it raise site cross-scripting errors?
Well basically, it is because we pass back a function that defines an array containing the data for the autocomplete list rather then just the data itself. This then allows the returned object to be used with a <script> element, via a process known as “script element injection” . There is a lot more to it then that, so rather than duplicate information from other sources, here are some links:
Same origin policy: http://en.wikipedia.org/wiki/Same_origin_policy
Why is the service free, and is it totally free – no ads, no links, nothing?
Yes it is totally free – no ads, no links, nothing. You may have notice that we offer a number of free and mostly free services, and hopefully you also noticed that we also offer an even larger number of paid services – well the short answer is that we make our money out of our paid services, and gain recognition via our free services which helps us sell more paid services. So we really don’t mind doing it, and besides we have been in this business for well over 10 years now (since 1999) and since the beginning we have always tried to give something back, and this philosophy has served us well.
How do you limit the search results to a country, state, or region?
Just add &filter=XX,YY,ZZ where XX,YY,ZZ is a comma delimited list of the countries TLDs that you wish to limit your result to. So for example, if you wanted to only list cities within the United States and Canada, then you would update the getJSON call to something like this:
Plus you can limit the autocomplete results even further by adding the state or region code as well – so for example you could limit the city list to cities within California by setting the filter parameter to USCA – which would look something like this:
You may notice that “USCA” is actually the Geobytes Location Code for California – try typing San Francisco into the above city text-box and then checking the “Region Code” in the City’s details table – this is a good way to find out the codes for other places – including those that are outside the US.
How to truncate off the state or country from a filtered list
If you do limit the results to a state or country, then you may wish to truncate off the state or country part of the response – to save it being repeated all of the time in the drop-down list. You can do this via the template parameter – which allows you to use “geobytes tags” to specify the format of the response.
For example, if you wanted to include only the city and state code in the response, then you could pass in a template like this.
Is the search limited to the first few letters or does it search the entire city name, including the country name?
By default it will only match on the start of the city name, however if you pass in fulltext=”true” then it will first search for an exact match on the prefix of the city name, and then search for a partial match any where within the full name of the city, including the country name.
Can the larger cities be sorted to the top?
Yes, by default the Autocomplete list is sorted by the fully qualified city name, but if you prefer that it be sorted by the size of the city then you pass in sort=size which will cause the larger cities within the list to be sorted to the top.