简体   繁体   中英

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

I can get the details from here 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 .

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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