Search code examples
jqueryapigeonames

geonames exception


I am using Geonames web service for registration on my website. If an error occurs I want to send myself an email with the error. Geonames returns exceptions http://www.geonames.org/export/webservice-exception.html but I'm not sure how to display them.

I'm using Geonames in Jquery for auto-completing locations when users enter them.

$( "#location" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        q: request.term,
                        featureClass: "P",
                        style: "full",
                        maxRows: 10 
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {                                                           
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name,
                                latitude: item.lat,
                                longitude: item.lng,
                                status: item.status
                            }
                        }));  

In the success function the last parameter status should return the status of the request. I appended that to a div but nothing appears. Please help me.. I'm not sure what I'm doing wrong.


Solution

  • I played around quite a bit with this one and I think you're overthinking it. It looks to me like the only situation in which your JSON response is going to contain a status attribute is when the HTTP response is not 200.

    For example, when you get a 503 from the server for the service being overloaded, that success function never actually gets run. From what I can tell it looks like you will need to add error: function( data ) {...} to be able to access the status option. Hope that was helpful!