为什么我的Array在AJAX函数之外表现不同? (填充jsTree) [英] Why is my Array behaving differently outside of an AJAX function? (populating jsTree)

查看:89
本文介绍了为什么我的Array在AJAX函数之外表现不同? (填充jsTree)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我正在为jsTree创建一个类似JSON的数组。我不明白的是为什么数组在AJAX成功函数中完全符合我的需要,但在该函数的。从我的控制台转储中查看屏幕截图,您可以看到差异。为什么函数内部与函数外部不同?

Long story short, I'm creating a JSON-like array for jsTree. What I don't understand is why the array is perfect for my needs inside the AJAX success function, but broken outside of that function. Check out the screenshot from my console dump, you can see the differences. Why is it different inside of the function vs. outside of the function?

基本上,我不能做我需要做的事情,除非它是完美的格式:(函数内的myAry)

Essentially, I can't do what I need to do unless it's in the perfect format: (myAry inside the function)

什么给人,男人?

var myAry = [];

$.ajax({
type: "GET",
url: "parents.xml",
dataType: "xml",
success: function(xml) {

    $(xml).find('group').each(function() {

      myAry.push({
        "id": $(this).find('GroupID').text(),
        "parent": "#",
        "text": $(this).find('GroupName').text(),
      });


    }); //end each loop

      //this array is PERFECT
      console.log(myAry);

      } //end success function
  }); //end ajax GET

  //THIS ARRAY IS BORKED
  console.log(myAry);






xml:


xml:

 <groups>
    <group>
        <GroupID>1</GroupID>
        <GroupName>Instructional Assistant</GroupName>
    </group>
    <group>
        <GroupID>2</GroupID>
        <GroupName>Technician</GroupName>
    </group>
    <group>
        <GroupID>3</GroupID>
        <GroupName>HR Specialist</GroupName>
    </group>
</groups>

推荐答案

它命中第一行代码;

//这个阵列是BORKED
console.log(myAry);

Ajax调用返回进程之前的异步Javascript

Asynchronous Javascript before the Ajax call returns processes

这篇关于为什么我的Array在AJAX函数之外表现不同? (填充jsTree)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆