Jquery错误函数返回异常消息
我有一个自动完成输入功能,访问者可以在其中输入位置,并在您键入时提供建议。我为此使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Geonames 将在成功处理程序中返回此错误信息,而不是错误处理程序。所以你必须在你的成功函数中询问:
希望这有帮助。干杯
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
item 尚未在该范围内声明。试试这个:
item hasn't been declared in that scope. Try this: