javascript可以在调试器中工作,但不能在运行模式下工作
我正在使用google.api验证地址,然后进行ajax调用,如果地址有效,则将其插入数据库。但是google代码是在if((lati==未定义)和&(longi==未定义))之后执行的,其地址设置无效。在检查lati和longi是否未定义之前,请说明为什么不执行google.mapjavascript可以在调试器中工作,但不能在运行模式下工作,javascript,Javascript,我正在使用google.api验证地址,然后进行ajax调用,如果地址有效,则将其插入数据库。但是google代码是在if((lati==未定义)和&(longi==未定义))之后执行的,其地址设置无效。在检查lati和longi是否未定义之前,请说明为什么不执行google.map $( document ).ready(function() { var geocoder = new google.maps.Geocoder(), lati,longi; $("#editDis
$( document ).ready(function() {
var geocoder = new google.maps.Geocoder(), lati,longi;
$("#editDistributor").dialog({
autoOpen: false,
height: 550,
width: 550,
modal: true,
buttons: {
"save": function() {
if ($( "#distAddress" ).val()) {tempAddress = $( "#distAddress" ).val();}
if($( "#distCity" ).val()) tempAddress = tempAddress + $( "#distCity" ).val();
if($( "#distState" ).val()) tempAddress = tempAddress + $( "#distState" ).val();
if($("#distZip").val()) tempAddress = tempAddress + $("#distZip").val();
geocoder.geocode({'address': tempAddress }, function(results, status) {
if(status) {
google.maps.GeocoderStatus.OK:
lati=results[0].geometry.location.k;
longi=results[0].geometry.location.D;
}
});
if ((lati === undefined) && (longi=== undefined)){
alert ( "Invalid Address");
}else{
var dialog = $(this);
var mapContent = new Object();
if($( "#distCompany" ).val() != undefined){
mapContent.name=$( "#distCompany" ).val();
}else{
alert("Please enter company Name");
}
$.ajax({
type: "POST",
url: window.location.origin+"/"+window.location.pathname.split("/")[1]+"/map/create",
data: JSON.stringify(mapContent),
dataType: "html",
contentType: 'application/json; charset=utf-8',
success: function(response){
alert("yes");
},
error: function(){
alert("Failure");
}
});
}
},
close: function() {
$(this).dialog("close");
}
}
});
感谢如果您使用异步或延迟将google maps脚本添加到页面,则在调用
文档时,脚本依赖项将不会加载。ready
回调被调用,但这只是一个猜测,没有看到您正在使用的标记。抱歉,我没有了解Youzzbov是对的,您必须将第二篇ajax文章放在geocode回调中。异步调用在代码的主流之后执行。编辑:在主流之后调用回调结果。编辑2:document.ready不是问题所在,在完成对geocode的请求后,您将获得lati和longi变量,同时您的代码将继续运行,并且您将在执行变量之前对其进行评估setted@G-主机,而不查看其余代码或错误消息,而且知道GoogleMaps脚本可能会在文档准备就绪后调用,新的google.maps.Geocoder()
可能会失败。因为这个问题没有包含足够的信息来正确地调试这个问题,所以谢谢大家,它确实有效。