I have an auto-complete input where visitors enter a location and it provides suggestions while you type. I’m using the Geonames web server for this.
I’m trying to return an exception message if an error occurs on the Geonames web server. For example if I exceed my web server request limit I want the script to send me an email alert.
The exception message returned is JSON.
This is part of the script with the error() function. I deliberately made a typo in the username parameter in data to get an error but no alert appears.
$(function() {
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://api.geonames.org/searchJSON",
dataType: "jsonp",
data: {
q: request.term,
countryBias: "US",
featureClass: "P",
style: "full",
maxRows: 10,
ussername: "demo",
operator: "OR"
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name,
saved_country: item.countryName,
saved_state: item.adminName1,
saved_city: item.name,
saved_zipcode: item.postalcode,
saved_latitude: item.lat,
saved_longitude: item.lng,
}
}));
},
error: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
request_status: item.status
}
alert("Error msg!");
}));
},
});
},
Geonames will return this error information in the success hundler, not the error handler. So you’d have to ask inside your success function:
Hope this helps. Cheers