dongqian6484 2013-08-23 10:53
浏览 46
已采纳

从Jquery $ .getJSON方法获取全局数组数组到REUSE数组

Okay so after searching Stack Overflow for answers I found two threads and tried them both but they did not work for me.

Assign data from jQuery getJSON to array

How to get JSON array from file with getJSON?

My issues is I want to work with some data from a database for a mapping utility, however some elements of the JSON dissappear after you call the Jquery method $.getJSON(). I can create them to a 'ul' element just fine. I CANNOT get them to push to an array of array's with any method I have tried yet. I am asking here as I am curious what I am doing wrong. I have thought I may have to traverse the DOM to get the elements once populated maybe but that seems silly to me and I was wondering if there was an easier way potentially. I am a novice at javascript and Jquery but understand programming concepts.

The concepts I am using are this:

  1. I have a SQL Server 2008 database with values:

    PlaceID PlaceName
    1       Place 1
    2       Place 2
    3       Place 3
    4       Place 4
    
  2. I can create a PHP script with the 'sqlsrv' driver to get the values and output a

    echo json_encode($data);
    
  3. I can confirm I can return the data from this PHP script in IIS locally. (with all the special jerry rigging you have to do to IIS to make it like PHP)

  4. I can call the php script and display it's data with JQuery library 2.0.3.js using the

    $.getJSON('SqlTalk.php')
    
  5. I can populate elements in the HTML with this, but I cannot get an array of array's for reuse with other objects, which is what I really really want. Ultimately I want to make an entire javascript that just gets an array of arrays from the PHP script and then reference that javascript either embedded or as a reference to use for mapping. However it appears $.getJSON is asychrnonous from my reading but even the methods I am seeing are not working for me to attempt to 'push' to an existing array. It may be that I am misunderstanding syntax when mixing Jquery and traditional javascript as well though.

Complete HTML code where the bottleneck is. Basically PHP will be returning data as shown in step 1 except as a JSON object. This is a simple example to get me beyond proof of concept first. I can push explicitly to the array, and I can generate the child items of the ul element just fine. I cannot push the items from 'getJSON' at all no matter what I attempt at reformating the getJSON method from what I have read.

<html>
<head>
        <script type='text/javascript' src='JQuery 2.0.3.js'></script>
</head>
<body>
    <ul></ul>
    <script>
        test = [
                {
                  PlaceID: "1",
                  PlaceName: "Somewhere"
                }
               ]

        test.push({PlaceID: "2", PlaceName: "SomewhereElse"});

        function getArray() {
            return $.getJSON('SqlTalk.php')
        }

        getArray().done(function(json){
           $.each(json, function(key, val){
              //test[key] = { PlaceID: val.PlaceID};  // doesn't work
              test.push({PlaceID: val.PlaceID, PlaceName: val.PlaceName});  // also does not work, can't push.
              $('ul').append('<li id="' + key + '">' + val.PlaceName + '</li>');
           });
        });

        alert(test.length);
        // So I get my output to the 'ul' element fine, but my test array never gets the values.
        //I have tried what others have stated and it does not work.
   </script>

</body>
</html>

展开全部

  • 写回答

2条回答 默认 最新

  • drelgkxl93433 2013-08-23 11:25
    关注

    The global will not be updated until after the request is complete.

        var getArrayPromise = getArray().done(function(json){
           $.each(json, function(key, val){
              //test[key] = { PlaceID: val.PlaceID};  // doesn't work
              test.push({PlaceID: val.PlaceID, PlaceName: val.PlaceName});  // also does not work, can't push.
              $('ul').append('<li id="' + key + '">' + val.PlaceName + '</li>');
           });
        });
    
        getArrayPromise.done(function(){
            alert(test.length);
        });
    

    there is no workaround, that's just how asynchronous requests work. The clock keeps ticking and the code keeps executing while the request is being received.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部