0

我目前正在使用 jquery 和回调函数进行 AJAX 调用,以检索 AJAX 调用之外的结果,并且在尝试使用循环从此处提供的我的 json 文件(ticker.json)中打印更多数据时遇到了麻烦:

{
    "test": {
        "msgOne": [
            "Remote One",
            "Remote Two",
            "Remote Three"
        ],
        "msgTwo": "Remote2",
        "msgThree": "Remote3"
    }
}

我的代码也在下面:

<html>
<head>
<title>Weather Data for Emergency Models</title>
<script src="jquery-1.9.1.min.js" type="text/javascript"></script> 
</head>

<body>
<script Language="JavaScript">

    function hmm(callback) {

        $.ajax({
            url : 'ticker.json',               //   ___ |I want to loop this
            dataType: 'json',                  //   |   |index as a variable  
            success: function(response) {      //   v   
                result = response['test']['msgOne'][2];
                callback(result);
            }
        });

    }

    hmm(function(result) {
        document.write(result);  //currently outputs as "Remote Three"
    });


</script>
</body>
</html>

主要问题是我想继续异步使用回调函数并循环遍历 json 文件中的“msgOne”数组并将所有三个结果按顺序打印到网页。我之前曾尝试在多个地方引入 for 循环,但我不断收到错误。我意识到还有其他方法可以做到这一点,但是在想要的条件下(异步和回调函数,因为我想最终将它应用于 jsonp 以在列表中的多个网站上找到 json 文件),有没有办法做到这一点?我最终想修改给定的代码来处理数组和更复杂的代码。

4

4 回答 4

0
    $.ajax({
        url : 'ticker.json',               //   ___ |I want to loop this
        dataType: 'json',                  //   |   |index as a variable  
        success: function(response) {      //   v   
            var result = response['test']['msgOne'];
            $.each(result,callback ).   

        }
    }); 
  function callback(index ,data){
   document.write(data);
  }
于 2013-05-15T19:46:05.780 回答
0

试试这个假设 response['test']['msgOne'] 是一个数组

 success: function(response) {
                  $.each(response['test']['msgOne'], callback);
            }



hmm(function(i, result) {
        document.write(result);  //currently outputs as "Remote Three"
    });
于 2013-05-15T19:35:33.367 回答
0

试试这个 - 在你的success

success: function(response) {      
  callback(response);
}

在你的function

hmm(function(result) {
    $.each(result.test.msgOne,function(i,v){
     document.write(v);
    });
});
于 2013-05-15T19:37:11.433 回答
0

这帮助我遍历 div 行,提取它们的 data-id 属性并使用该数据 id 从 ajax 调用中检索数据。碰巧有 40 多个 ajax 调用需要遍历。我必须让它们保持异步真实,但要使调用失败,以免服务器超载。我还使用 PHP 检索缓存数据并将 PHP 缓存数组转换为 javascript 对象:

 public static function load_footer_js_css() {
        $screen = get_current_screen();

        $whitelist = array('post','page');

        if(!isset($screen) || !in_array($screen->post_type , $whitelist )) {
            return;
        }

        $transient = get_transient( 'inbound_ga_post_list_cache' );
        $js_array = json_encode($transient);


        ?>
        <script type="text/javascript">
            <?php
            echo "var cache = JSON.parse('". $js_array . "');\n";
            ?>

            function inbound_ga_listings_lookup( cache, post_ids, i , callback , response ) {

                if (!post_ids[i]){
                    return true;
                }

                if (typeof response == 'object' && response  ) {
                    jQuery('.td-col-impressions[data-post-id="' + post_id + '"]').text(response['impressions']['current']['90']);
                    jQuery('.td-col-visitors[data-post-id="' + post_id + '"]').text(response['visitors']['current']['90']);
                    jQuery('.td-col-actions[data-post-id="' + post_id + '"]').text(response['actions']['current']['90']);
                }

                if (i == 0) {
                    post_id = post_ids[0];
                    i++;

                } else {
                    post_id = post_ids[i];
                    i++;
                }

                if (typeof cache[post_id] != 'undefined') {
                    jQuery( '.td-col-impressions[data-post-id="' + post_id + '"]').text( cache[post_id].impressions.current['<?php echo self::$range; ?>'] );
                    jQuery( '.td-col-visitors[data-post-id="' + post_id + '"]').text(cache[post_id].visitors.current['<?php echo self::$range; ?>']);
                    jQuery( '.td-col-actions[data-post-id="' + post_id + '"]').text(cache[post_id].actions.current['<?php echo self::$range; ?>']);
                } else {
                    jQuery.ajax({
                        type: "POST",
                        url: ajaxurl,
                        data: {
                            action: 'inbound_load_ga_stats',
                            post_id: post_id
                        },
                        dataType: 'json',
                        async: true,
                        timeout: 10000,
                        success: function (response) {

                            callback(cache, post_ids, i, callback , response);
                        },
                        error: function (request, status, err) {
                            response['totals'] = [];
                            response['totals']['impressions'] = 0;
                            response['totals']['visitors'] = 0;
                            response['totals']['actions'] = 0;
                            callback(cache, post_ids, i, callback , response);
                        }
                    });
                }
            }

            jQuery(document).ready( function($) {

                var post_ids = [];
                var i = 0
                jQuery( jQuery('.td-col-impressions').get() ).each( function( $ ) {
                    var post_id = jQuery(this).attr('data-post-id');
                    post_ids[i] = post_id;
                    i++;
                });

                inbound_ga_listings_lookup( cache, post_ids, 0 , inbound_ga_listings_lookup , null );
        });
        </script>
        <?php
    }
于 2016-03-18T03:28:21.653 回答