【发布时间】:2013-12-19 21:46:10
【问题描述】:
我无法使用 Google 反向地理编码 api 触发 ajax 函数的“错误:”部分。
http://codepen.io/mustachios/pen/sdwIG
大多数邮政编码都可以,但是如果您输入的字符串太长(例如“12345678”),则不会触发错误警报,控制台只会显示我的结果未定义。
我应该只在函数的“成功:”部分检查未定义吗?
【问题讨论】:
标签: jquery ajax google-maps-api-3
我无法使用 Google 反向地理编码 api 触发 ajax 函数的“错误:”部分。
http://codepen.io/mustachios/pen/sdwIG
大多数邮政编码都可以,但是如果您输入的字符串太长(例如“12345678”),则不会触发错误警报,控制台只会显示我的结果未定义。
我应该只在函数的“成功:”部分检查未定义吗?
【问题讨论】:
标签: jquery ajax google-maps-api-3
success:function(result){
//console.log(result);
if(result.results[0]){
var addresses = result.results[0].address_components;
}
else{
alert('error');
}
如果邮政编码不存在 result.results[0].address_components 不存在,则 result.results[0] 将是未定义的。因此,在用户单击“提交”后,通过if(result.results[0]) 检查 result.results[0] 是否存在,如果存在则分配 addresses,如果不存在则分配 alert('error')。
【讨论】:
failure-callback,它必须是 error
OK ,否则没有结果。【讨论】: