在jquery中的AJAX调用中循环访问URL [英] Looping through the URL in an AJAX call in jquery

查看:134
本文介绍了在jquery中的AJAX调用中循环访问URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最近问了一个有关使用回调函数通过jquery中的异步AJAX调用遍历json文件中的数据的问题(

So I recently asked a question concerning looping through data in a json file from an asynchronous AJAX call in jquery that uses callback functions (Looping through AJAX and callbacks in jquery) and I got a lot of good help, but now I wonder if I were to have 3 different json files (same structure and naming convention, but holding slightly different data) and their URLs in an array called myURL, would there be a way to loop through the AJAX call for all three URLs and output each of their respective results on a webpage? Below is my code:

<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">

    //New Array for my URLs
    var myURL =  ["ticker1.json","ticker2.json","ticker3.json"];

    function hmm(callback) {

        $.ajax({
            url : myURL[j],  //<<<---Want to loop through this array
            dataType: 'json',
            success: function(response){
                    callback(response);
            }
        });

    }

    hmm(function(result) {
        $.each(result.test.msgOne,function(i,v){
        document.write(v);
        document.write('<br>');
        });
    });

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

推荐答案

尝试一下

for(var k=0,len=myURL.length; k<len; k++)   

            $.ajax({
                url : myURL[k++],  //<<<---Want to loop through this array
                dataType: 'json',
                success: function(response){
                        callback(response);
                }
            });
}

这篇关于在jquery中的AJAX调用中循环访问URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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