开发者

Jquery error function to return exception message

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:

success: function( data ) {
    if(typeof(data.status) != 'undefined'){ //It means there was an error
       var errorObject = data;
       //Now you have access to errorObject.status, errorObject.status.message and so on
       //Do something with your error object
       return; //Avoid execution of the rest of the code of this function
    } //Can add an else here if you want
    response( $.map( data.geonames, function(item){
      ...

}

Hope this helps. Cheers


item hasn't been declared in that scope. Try this:

error: function( data ) {  
    var request_status= data.status;  
    //do stuff with status  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜