forked from Eished/JavaScript_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.ajax.html
More file actions
64 lines (61 loc) · 1.67 KB
/
23.ajax.html
File metadata and controls
64 lines (61 loc) · 1.67 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<html>
<head>
<script type="text/javascript">
function Ajax(url, suc, err) {
var oAjax = '';
if (window.XMLHttpRequest) {
// chrome FF IE9
oAjax = new XMLHttpRequest;
} else if (window.ActiveXObject) {
oAjax = new ActiveXObject("Microsoft.XMLHTTP");
}
if (oAjax != '') {
oAjax.onreadystatechange = state_change;
oAjax.open('get', url, true);
oAjax.send();
} else {
err();
}
function state_change() {
if (oAjax.readyState === 4) {
// 完成
if (oAjax.status === 200) {
// 成功
if (suc) {
suc(oAjax.responseText);
} else {
return oAjax.responseText;
}
} else if (oAjax.status === 404){
console.log('url错误或不存在');
} else if (err){
err();
}
} else if (oAjax.readyState === 0){
console.log('未初始化');
} else if (oAjax.readyState === 1){
console.log('正在发送请求');
} else if (oAjax.readyState === 2){
console.log('处理请求中');
} else if (oAjax.readyState === 3){
console.log('正在解析');
}
}
}
window.onload = function () {
var btn = document.getElementById('btn');
btn.onclick = function () {
Ajax('/api/blog/list', function (str) {
// 使用返回数据
console.log('成功')
document.getElementById('T1').innerHTML = str;
})
}
}
</script>
</head>
<body>
<div id="T1" style="border:1px solid black;height:40;width:300;padding:5"></div><br />
<button id="btn">Click</button>
</body>
</html>