Jquery中getJson函数用法实例详解
2014-08-18 17:26 魔术G 阅读(569) 评论(0) 收藏 举报本文分别介绍访问json的数据和json的数据格式两种方式以及用ajax和getjson实现的两种方法。
1、访问的json数据 test.json
{
"foo": "The quick brown fox jumps over the lazy dog.",
"bar": "ABCDEFG",
"baz": [52, 97]
}
第一种方式:
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback //返回成功时调用函数callback
});
第二种方式:
$.getJSON('ajax/test.json', function(data) {
$('result').html('<p>' + data.foo + '</p>'
+ '<p>' + data.baz[1] + '</p>');
});
2、访问的json数据格式 test.json
[ { "foo": "The quick brown fox jumps over the lazy dog...",
"bar": "ABCDEFG",
"baz": 42
}, {
"foo": "The quick brown fox jumps over the lazy dog.",
"bar": "WWWWWWWWWWWWWWWWWW",
"baz": 67
},.... ]
<script>
$.getJSON("test.json",
{
tags: "cat",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(i,item){
//相当于循环输出json数组中数据
});
});
</script>
浙公网安备 33010602011771号