我目前正在使用 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 文件),有没有办法做到这一点?我最终想修改给定的代码来处理数组和更复杂的代码。