从XML源创建对象的JavaScript数组

从XML源创建对象的JavaScript数组,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我正在成功地从一些XML创建对象。然后,我尝试将每个新对象放入数组的新索引中,该数组最终将包含所有对象 但是,数组始终返回为空。我的代码如下: var $locations = []; /*$obj = {}; $obj['test'] = 'working'; $locations.push($obj);*/ $.ajax({ type:

我正在成功地从一些XML创建对象。然后,我尝试将每个新对象放入数组的新索引中,该数组最终将包含所有对象

但是,数组始终返回为空。我的代码如下:

var $locations  =   [];
            /*$obj  =   {};
            $obj['test']    =   'working';
            $locations.push($obj);*/

            $.ajax({
                type:       "POST",
                url:        "/locations/845/data.xml",
                dataType:   "xml",
                success:    function($xml){

                    $($xml).find('node').each(
                        function(){
                            $location   =   {};
                            //alert( $(this).attr('Latitude') );
                            $location['latitude']   =   $(this).attr('Latitude');
                            $location['longitude']  =   $(this).attr('Longitude');
                            $location['city']       =   $(this).attr('City');
                            $location['street']     =   $(this).attr('Street');

                            //alert( $location.toSource() );
                            //alert( $location['latitude'] );
                            $locations.push($location);
                        }
                    );
                }
            });
            alert( $locations.toSource() );

创建并插入到$locations数组中的注释对象是一个测试,它可以正常工作。但是ajax成功函数中实际有用的代码不是这样的。

您的ajax调用是异步的。当您调用它时,它只是开始执行它,其余的代码继续运行。当您发出警报时,ajax尚未完成,在调用成功处理程序函数之前,ajax尚未完成。您可以知道ajax调用完成的唯一地方是成功处理程序本身。事实上,您想要对返回的ajax数据执行的任何操作都应该从成功处理程序启动,而不是从调用ajax调用后执行的代码启动

如果移动线:

alert( $locations.toSource() );
在success handler函数的末尾,您将看到您的数据。只有到那时,ajax调用才真正完成

试着这样做:

        var $locations  =   [];
        /*$obj  =   {};
        $obj['test']    =   'working';
        $locations.push($obj);*/

        $.ajax({
            type:       "POST",
            url:        "/locations/845/data.xml",
            dataType:   "xml",
            success:    function($xml){

                $($xml).find('node').each(
                    function(){
                        $location   =   {};
                        //alert( $(this).attr('Latitude') );
                        $location['latitude']   =   $(this).attr('Latitude');
                        $location['longitude']  =   $(this).attr('Longitude');
                        $location['city']       =   $(this).attr('City');
                        $location['street']     =   $(this).attr('Street');

                        //alert( $location.toSource() );
                        //alert( $location['latitude'] );
                        $locations.push($location);
                    }
                );
                alert( $locations.toSource() );
            }
        });

您的ajax调用是异步的。当您调用它时,它只是开始执行它,其余的代码继续运行。当您发出警报时,ajax尚未完成,在调用成功处理程序函数之前,ajax尚未完成。您可以知道ajax调用完成的唯一地方是成功处理程序本身。事实上,您想要对返回的ajax数据执行的任何操作都应该从成功处理程序启动,而不是从调用ajax调用后执行的代码启动

如果移动线:

alert( $locations.toSource() );
在success handler函数的末尾,您将看到您的数据。只有到那时,ajax调用才真正完成

试着这样做:

        var $locations  =   [];
        /*$obj  =   {};
        $obj['test']    =   'working';
        $locations.push($obj);*/

        $.ajax({
            type:       "POST",
            url:        "/locations/845/data.xml",
            dataType:   "xml",
            success:    function($xml){

                $($xml).find('node').each(
                    function(){
                        $location   =   {};
                        //alert( $(this).attr('Latitude') );
                        $location['latitude']   =   $(this).attr('Latitude');
                        $location['longitude']  =   $(this).attr('Longitude');
                        $location['city']       =   $(this).attr('City');
                        $location['street']     =   $(this).attr('Street');

                        //alert( $location.toSource() );
                        //alert( $location['latitude'] );
                        $locations.push($location);
                    }
                );
                alert( $locations.toSource() );
            }
        });

Ajax是异步的。您的警报在Ajax调用完成之前显示。为什么要将
$
放在JS变量前面???您好,Cipi,我只在JS变量前面使用$,因为我习惯于在编写PHP代码时使用$。无论您是否使用异步的$Ajax,都没有区别。您的警报在Ajax调用完成之前显示。为什么要将
$
放在JS变量前面???您好,Cipi,我只在JS变量前面使用$,因为我习惯于在编写PHP代码时使用$。无论你是否使用$Thank you,都没有区别!我在自责,因为我早该知道的。谢谢你的帮助,谢谢!我在自责,因为我早该知道的。我感谢你的帮助。