Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.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回调_Javascript_Jquery_Asp.net_Asynchronous_Callback - Fatal编程技术网

异步JavaScript回调

异步JavaScript回调,javascript,jquery,asp.net,asynchronous,callback,Javascript,Jquery,Asp.net,Asynchronous,Callback,我似乎无法解决这个问题 我使用Maxmind GeoIP2 JavaScript API和异步回调返回纬度、经度和细分或区域 <script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js"></script> <!--Html tags ... --> <script type="text/javascript">

我似乎无法解决这个问题

我使用Maxmind GeoIP2 JavaScript API和异步回调返回纬度、经度和细分或区域

    <script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js"></script>
    <!--Html tags ... -->
    <script type="text/javascript">
        geoip2.city(
            function (response) {
                var latitude = "";
                var longitude = "";
                var region = "";

                latitude = response.location.latitude;
                longitude = response.location.longitude;
                region = response.subdivisions[0].iso_code;

                //Other operations.                       
            },
            function (error) {
                try {
                    console.log(error);
                }
                catch (ex) {
                    alert(ex);
                }
            }
        );
    </script>
    <!--Html tags ... -->
    <script type="text/javascript">
        $(document).ready(function () {
            //Synchronous JavaScript against the DOM.
        });
    </script>

geoip2.1城市(
功能(响应){
var纬度=”;
var经度=”;
var区域=”;
纬度=响应。位置。纬度;
经度=response.location.longitude;
区域=响应。细分[0]。iso_代码;
//其他行动。
},
函数(错误){
试一试{
console.log(错误);
}
捕获(ex){
警报(ex);
}
}
);
$(文档).ready(函数(){
//针对DOM的同步JavaScript。
});
然后,这些值应返回并写入ASP.NET Web表单更新面板中的DOM,该面板将自动回发到服务器。然后,服务器在自定义数据库中进行查找,并返回指向Google地图的最近50个左右的位置,该地图在回发时通过传递jQuery document ready anon函数呈现

当然,事实并非如此。并非每件事都是按顺序发生的。(我不希望这样,我只需要知道解决这个问题的正确方法。)

我还想补充几点:

  • 它在Maxmind从旧的同步更改之前工作
    JavaScript API调用此异步回调API
  • 这不是我的代码或方法。我继承了这种美

  • 您只需要将同步内容嵌套在任何异步回调中。异步编程的关键是记住JS中的函数只是对象,可以传递

    因此,您可以预先定义所有回调,然后将它们链接到另一个回调中。下面是一个示例(我假设您的数据库
    lookup
    是同步的),因此,如果没有完全意义,请道歉,但给出了如何执行异步位的想法

    将所有内容放入
    $(document.ready)
    中,而不是将其拆分为两个脚本:

    $(document).ready(function () {
    
        var weHaveFailed = function(){
            // do sad things
        };
    
        var getLocation = function(successCallback, failureCallback){
          geoip2.city(
            function (response) {
              // success!! Unpack the response
              var latitude = response.location.latitude;
              var longitude = response.location.longitude;
              var region = response.subdivisions[0].iso_code;
    
              // ... do other stuff
              // then callback the passed in successCallback function with your coordinates
              // and the function we want to execute when THAT is successful
              successCallback(latitude, longitude, region, updateTheLocations);                   
            },
            function (error) {
              // failure :(
              console.log(error.message);
              failureCallback();
            }
          );
    
        var lookup = function(lat, long, reg, successCallback){
          var locations = [];
          // ...get the data from the database, is this sync?
          // ...assign the response to our locations array
          // ... and call the passed in successCallback with the locations array
          successCallback(locations);
        };
    
        var updateTheLocations = function(locations){
          // We pass in the locations from the lookup call
          // do happy things, like synchronous JavaScript against the DOM
        };
    
        // start the ball rolling, passing in the function we want to run when geoip2 gets the data
        getLocation(lookup, weHaveFailed);
    
    });
    

    我相信这是解决问题的正确方法。为了回答你的问题,我没有实施它。目前,我已经将(时间限制)恢复为Maxmind GeoIP API对纬度、经度和区域的遗留调用。但您的输入非常感谢。其他希望使用旧的sync Maxmind GeoIP遗留JavaScript API而不是回调的用户的详细信息:。使用城市API和geoip_region()、geoip_latitude()、geoip_longitude()函数。如果您有时间来实现,我建议您使用回调和@drjimmie1976的方法。@user888298如果我们有时间(重新)编写我们想要的一切代码就好了!;)