Jquery错误函数返回异常消息

发布于 2024-11-05 07:05:04 字数 2299 浏览 3 评论 0原文

我有一个自动完成输入功能,访问者可以在其中输入位置,并在您键入时提供建议。我为此使用 Geonames Web 服务器。

如果 Geonames Web 服务器上发生错误,我会尝试返回异常消息。例如,如果我超出了 Web 服务器请求限制,我希望脚本向我发送电子邮件警报。

返回的异常消息是JSON。

这是带有 error() 函数的脚本的一部分。我故意在数据中的用户名参数中输入错误以得到错误,但没有出现警报。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-11-12 07:05:04

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

}

希望这有帮助。干杯

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

你曾走过我的故事 2024-11-12 07:05:04

item 尚未在该范围内声明。试试这个:

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

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

error: function( data ) {  
    var request_status= data.status;  
    //do stuff with status  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文