jQuery.getJSON() を使ったプログラムは次のようになります。
sample_getJSON.htmlという名前で保存します。また、あらかじめsimple.jsonファイルを用意しておきましょう。このようにプログラムを書くと、json変数にsimple.jsonのデータが読み込まれまるので、それを参照すればいいだけです。
<html>
<head>
<title>jQuery.getJSON sample</title>
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#btn").click(function(){
var uri = "./simple.json";
$.getJSON(uri, null, function(json, status){
for (i in json) {
$("#result").append(i + ": " + json[i]).append("<br/>");
}
});
});
});
</script>
</head>
<body>
<form>
<input type="button" value="show" id="btn" />
</form>
<div id="result"></div>
</body>
</html>
<head>
<title>jQuery.getJSON sample</title>
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#btn").click(function(){
var uri = "./simple.json";
$.getJSON(uri, null, function(json, status){
for (i in json) {
$("#result").append(i + ": " + json[i]).append("<br/>");
}
});
});
});
</script>
</head>
<body>
<form>
<input type="button" value="show" id="btn" />
</form>
<div id="result"></div>
</body>
</html>