0

訪問者が場所を入力するオートコンプリート入力があり、入力中に提案が表示されます。これにはGeonamesWebサーバーを使用しています。

Geonames Webサーバーでエラーが発生した場合、例外メッセージを返そうとしています。たとえば、Webサーバーの要求制限を超えた場合、スクリプトから電子メールアラートを送信する必要があります。

返される例外メッセージはJSONです。

これは、error()関数を使用したスクリプトの一部です。データのusernameパラメーターに意図的にタイプミスをしてエラーを取得しましたが、アラートが表示されません。

$(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!");
                        }));  
                    },                                              
                });             
            },
4

2 に答える 2

1

アイテムはそのスコープで宣言されていません。これを試して:

error: function( data ) {  
    var request_status= data.status;  
    //do stuff with status  
}
于 2011-05-06T22:50:15.000 に答える
1

Geonamesは、エラーハンドラーではなく、成功ハンドラーでこのエラー情報を返します。したがって、成功関数の内部で質問する必要があります。

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){
      ...

}

お役に立てれば。乾杯

于 2011-05-06T23:10:19.840 に答える