简体   繁体   English

使用jQuery从URL获取文件内容

[英]get file contents from a url using jquery

Is there any way to get the city and state name from zip code from google api using jquery(not using ajax). 有什么方法可以使用jquery(不使用ajax)从google api的邮政编码中获取城市和州名。

I can get the details from here http://maps.googleapis.com/maps/api/geocode/json?address=17078&sensor=true . 我可以从这里http://maps.googleapis.com/maps/api/geocode/json?address=17078&sensor=true获得详细信息。

and my code is 我的代码是

var googleAPI = "http://maps.googleapis.com/maps/api/geocode/json?address=17078&sensor=true";

$.getJSON(googleAPI, function (response) {

    console.log("JSON Data: " + response.items);
    for (var i = 0; i < response.items.length; i++) {
        var item = response.items[i];
        // in production code, item.text should have the HTML entities escaped.
        //document.getElementById("content").innerHTML += "<br>" + item.volumeInfo.title;

        alert(item.address_components);
      }
});

but this is not working 但这不起作用

You have to find the administrative_area_level_1 and administrative_area_level_2 which gives you the state and city names . 您必须找到为您提供statecity namesadministrative_area_level_1administrative_area_level_2

Read Geocoding docs 阅读地址解析文档

Try this, 尝试这个,

<script>
    // let you got the items.address_components 
    for(var a=0,len=items.address_components.length;a<len;a++)
    {
        ac=items.address_components[a];
        if(ac.types)
        {
            for(var t=0,tl=ac.types.length;t<tl;t++)
            {
                ty=ac.types[t];
                if(ty=='administrative_area_level_1')
                {
                    alert('State'+ac.long_name);
                }
                if(ty=='administrative_area_level_2')
                {
                    alert('City'+ac.long_name);
                }
            }
        }
    }            
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM