我可以在浏览器(firefox)中输入以下内容:http://maps.google.com/maps/api/geocode/json?address=91505&sensor=false并显示json数据。
当我尝试将其作为jquery ajax调用执行时,执行错误部分:
$(“#coZip”)。blur(function(){
var el = $(this);
$.ajax({
url: "http://maps.google.com/maps/api/geocode/json",
cache: false,
dataType: "json",
type: "GET",
data: "address=" + el.val().substring(0,5)+"&sensor=false",
success: function(result, success) {
$("#coCity").val(result.results[0].address_components[1].short_name);
$("#coState").val(result.results[0].address_components[3].short_name);
},
error: function(result, success) {
alert("zip error");
},
});
});
我已通过在警告窗口中显示数据字符串来验证数据字符串是“address = 91505& sensor = false”,因此我不知道为什么这不起作用。我得到子字符串的原因是因为我正在使用的掩码插件,#coZip的格式是91505- _ _。我不关心后缀,所以我将其消除。
我成功使用这个ajax调用,它也返回json数据:
$.ajax({
url: "http://zip.elevenbasetwo.com",
cache: false,
dataType: "json",
type: "GET",
data: "zip=" + el.val().substring(0,5),
success: function(result, success) {
$("#coCity").val(result.city);
$("#coState").val(result.state);
},
error: function(result, success) {
alert("zip error");
}
});
我想使用第一个ajax调用,因为第二个调用返回了许多可以被邮政编码使用的城市之一,但谷歌网站会返回正确的城市。 (例如90275)