Fork me on GitHub

jqueryAjax获取数据方法实现

通过jQuery 获取服务器数据 并且做解析,源码测试!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jquey Ajax 获取数据</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>

<body>
<div id="info"></div>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "https://api.apiopen.top/getJoke?page=1&count=2&type=video", // 免费 API 地址
dataType: "json", // 数据格式
type: "post", // 请求方式
async: false, // 是否异步请求
success: function (data) {
//如果请求成功,返回数据。
console.log(data) // 检测是否有返回值,
console.log(data.code) // 码
console.log(data.message) // 信息
console.log(data.result) // 结果

$("#info").html("") //清空info内容
$.each(data.result, function (i, item) {
$("#info").append(
"<div>" + "诗名:" + item.name + "</div>" +
"<div>" + "内容:" + item.passtime + "</div>" +
"<div>" + "作者:" + item.sid + "</div>" + "</br>" +
"<div>" + "作者:" + item.text + "</div>" + "</br>"
);
});
}
})
})
</script>
</body>

</html>

展示效果

tqcbZQ.png