Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 地理定位问题';s异步调用_Javascript_Jquery_Ajax_Asynchronous_Geolocation - Fatal编程技术网

Javascript 地理定位问题';s异步调用

Javascript 地理定位问题';s异步调用,javascript,jquery,ajax,asynchronous,geolocation,Javascript,Jquery,Ajax,Asynchronous,Geolocation,我正试图以Ajax请求的形式发送我的位置。 可能是由于地理定位的异步调用,它无法按预期工作。 Geolocation的函数在my outer函数之后调用,结果是数据为空答案 $(document).ready(function() { $("button").click(function() { var data = ""; if (...) { // if user wants to share their location

我正试图以Ajax请求的形式发送我的位置。
可能是由于地理定位的异步调用,它无法按预期工作。 Geolocation的函数在my outer函数之后调用,结果是
数据为空
答案

$(document).ready(function() {
    $("button").click(function() {

        var data = "";

        if (...) { // if user wants to share their location
            function geo_success(position) {
                console.info("lat:" + position.coords.latitude + " Lot: " + position.coords.longitude);
                data = {
                    lat : position.coords.latitude,
                    lon : position.coords.longitude
                };
            }

            function geo_error() {
                alert("No position available.");
            }

            var geo_options = {
                enableHighAccuracy : true,
                timeout : 1000,
                maximumAge : 0
            };

            navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
        }
        if (...) { // if user doesn't want to share their location
            data = {
                a : "abc", // some other data
                b : "def"
            };
        }
        if (...) { // maybe a third option
            data = {
                b : "test", // some other data
                c : "some data"
            };
        }

        if (Object.keys(data).length > 0) {
            console.info("hier");
            $.ajax({
                method : "GET",
                url : "test.php",
                data : data, // finaly send data
                dataType : 'json',
                success : function(data) {
                    console.log(data)
                },
                error : function(data) {
                    console.error(data);
                }
            });
        } else {
            console.error("data is empty");
        }
    });
});

<input type="checkbox" id="box1" />
<button>ok</button>
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
var数据=”;
如果(…){//if用户想要共享他们的位置
功能geo_成功(职位){
控制台信息(“纬度:+position.coords.latitude+”地块:+position.coords.latitude”);
数据={
纬度:位置坐标纬度,
lon:position.coords.longitude
};
}
函数geo_error(){
警报(“没有可用位置”);
}
变量geo_选项={
EnableHighAccurance:正确,
超时:1000,
最大值:0
};
navigator.geolocation.getCurrentPosition(地理位置成功、地理位置错误、地理位置选项);
}
如果(…){//if用户不想共享他们的位置
数据={
答:“abc”//其他一些数据
b:“def”
};
}
如果(…){//可能是第三个选项
数据={
b:“测试”//其他一些数据
c:“一些数据”
};
}
if(Object.keys(data.length>0){
控制台信息(“hier”);
$.ajax({
方法:“获取”,
url:“test.php”,
data:data,//最终发送数据
数据类型:“json”,
成功:功能(数据){
console.log(数据)
},
错误:函数(数据){
控制台错误(数据);
}
});
}否则{
控制台。错误(“数据为空”);
}
});
});
好啊
我无法将Ajax请求放入
geo\u success
,因为需要首先定义
数据。在我的原始代码中(这里只是一个摘录),根据用户的选择,有几种可能的
数据。
所以我只想要一个Ajax请求

可能的补救办法是什么

可能是由于地理定位的异步调用,它无法按预期工作。Geolocation的函数在我的外部函数之后被调用,导致一个数据为空的答案

你是说这是一个异步调用

navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
如果是这种情况,那么我怀疑您可以为它提供回调函数,以便在操作完成时执行。而且。。。看起来你已经这么做了

function geo_success(position) {
    console.info("lat:" + position.coords.latitude + " Lot: " + position.coords.longitude);
    data = {
        lat : position.coords.latitude,
        lon : position.coords.longitude
    };
}

function geo_error() {
    alert("No position available.");
}
因此,如果您需要响应异步操作的结果,只需将该代码放在这些回调函数中


异步意味着。。。不同步。异步操作完成后需要发生的任何事情都需要由该操作调用。在您的情况下,
数据
在您检查它时总是空的,因为您在创建它时显式地将它设置为空字符串,而当时没有任何其他内容更新它。

“我无法将Ajax请求放入geo_success,因为我需要先定义数据。”–我看不出这两件事之间有什么关系。在设置
数据
创建
promise
后,将ajax放入geo\u success,这将在
geo\u success
中解决,并在
geo\u error
中拒绝。然后在
中构建逻辑。然后
。您不能简单地将if/else条件语句放在geo success函数中吗?原因是AJAX是异步的,所以在从您的调用接收任何数据之前,浏览器已经计算了条件语句,然后移动您的逻辑。如果用户不同意,请使用测试数据覆盖原始数据对象。那么什么是解决方案?@user1170330:我提出的解决方案在某种程度上不起作用吗?如果我理解正确,您只是在描述问题。我错过了你的解决方案吗?@user1170330:很抱歉,我已经更新了答案。基本上,任何需要响应异步操作结果的代码都将进入回调函数。