[英]Looping through the URL in an AJAX call in jquery
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? 所以我最近问了一个有关使用回调函数的jquery异步AJAX调用中循环遍历json文件中数据的问题( 遍历Ajax和jquery中的回调 ),我得到了很多帮助,但是现在我想知道是否在名为myURL的数组中具有3个不同的json文件(相同的结构和命名约定,但持有的数据略有不同)和它们的URL,是否有一种方法可以循环遍历所有三个URL的AJAX调用并输出它们各自的结果在网页上? 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>
Try this 尝试这个
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);
}
});
}
$.each(myURL,function(i,url){
hmm(url,function(result) {
$.each(result.test.msgOne,function(i,v){
document.write(v);
document.write('<br>');
});
});
});
And in your function - 在您的职能中-
function hmm(url,callback) {
$.ajax({
url : url,
dataType: 'json',
success: function(response){
callback(response);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.