【问题标题】:Google Geocoding $.ajax()谷歌地理编码 $.ajax()
【发布时间】:2013-12-19 21:46:10
【问题描述】:

我无法使用 Google 反向地理编码 api 触发 ajax 函数的“错误:”部分。

http://codepen.io/mustachios/pen/sdwIG

大多数邮政编码都可以,但是如果您输入的字符串太长(例如“12345678”),则不会触发错误警报,控制台只会显示我的结果未定义。

我应该只在函数的“成功:”部分检查未定义吗?

【问题讨论】:

    标签: jquery ajax google-maps-api-3


    【解决方案1】:
    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')

    【讨论】:

      【解决方案2】:
      1. AFAIK 中没有 failure-callback,它必须是 error
      2. 地理编码器将始终返回有效的 JSON 和 HTTP 状态码 200,即使没有结果也是如此。您必须检查响应的状态属性,它必须是 OK ,否则没有结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多