2网络课笔记2封装ajax

"Hello World, Hello Blog"

Posted by wudimingwo on December 15, 2018

ajax : aysnchronous javascript and xml 特点: 异步,局部的发送网络请求, 不必刷新页面. image.png

image.png image.png

封装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function ajaxFun (method, url, data, callback, flag) {
           	var xhr = null;
           	var flag = typeof flag == "undefined" ? true : flag;
           	if (window.XMLHttpRequest) {
           		xhr = new XMLHttpRequest();
           	} else{
           		xhr = new ActiveXObject("Microsoft.XMLHttp");// 兼容 ie
           	}
           	method = method.toUpperCase();
           	if (method == "GET") {
           		xhr.open(method,url + "?" + data,flag);
           		xhr.send();
           	} else{
           		xhr.open(method, url, flag);
           		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
           		xhr.send(data);
           	}
           	
           	xhr.onreadystatechange = function () {
           		if(xhr.readyState == 4 && xhr.status == 200) {
           		  callback(xhr.responseText);
           		}
           	}
           }

简单案例

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
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewprot" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
        <title>dddd</title>
        <style type="text/css">
            
        </style>
    </head>
    <body>
         <form action="#" method="post">
         	<input type="text" name="" id="username" value="" />
         	<input type="text" name="" id="age" value="" />
         	<input id="aaa" type="submit" value="提交"/>
         	<input id="bbb" type="submit" value="获取" />
         </form>
         <ul id="ul"></ul>
        <script type="text/javascript">
            
            function ajaxFun (method, url, data, callback, flag) {
            	var xhr = null;
            	if (window.XMLHttpRequest) {
            		xhr = new XMLHttpRequest();
            	} else {
            	  xhr = new ActiveXObject("Microsoft.XMLHttp");
            	}
            	method = method.toUpperCase();
            	if (method == "GET") {
            		xhr.open(method,url + "?" + data,flag);
            		xhr.send();
            	} else if (method == "POST" ){
            		xhr.open(method,url,flag);
            		xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
            		xhr.send(data);
            	}
            	
            	xhr.onreadystatechange = function () {
            		if(xhr.readyState == 4 && xhr.status == 200) {
            		  callback(xhr.responseText);
            		}
            	}
            }
            
            const showlist = (data) => {
              console.log(JSON.parse(data));
              var list = JSON.parse(data);
              let str = "";
              list.forEach((item,index) => {
                str += "<li>" + item.title + "---" + item.date + "</li>"
              })
              ul.innerHTML = str;
            }
            
            bbb.onclick = function (e) {
            	e.preventDefault();
            	ajaxFun('GET','./getNews.php',null,showlist,true);
            }
            
            const showName = (data) => {
              alert(data);
            }
            
            aaa.onclick = function (e) {
            	e.preventDefault();
            	var name = username.value;
            	var agedata = age.value;
            	var data = "username=" + name + "&age=" + agedata;
            	ajaxFun('POST','./post.php',data,showName,true);
            }
            
            
        </script>
    </body>
</html>

下面这俩php我是不太懂的. 不过大概能看懂. post.php

1
2
3
4
5
6
7
8
9
<?php
	header('content-type:text/html;charset="utf-8"');
	error_reporting(0);
	
	$username = $_POST['username'];
	$age = $_POST['age'];

	echo "名字:{$username},年龄:{$age}";

getNews.php

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
<?php
	header('content-type:text/html;charset="utf-8"');
	error_reporting(0);
	
	
	$news = array(
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6'),
	array('title'=>'的空间发几块的价格卡减肥的恐 惧感','date'=>'2014-1-6')
	
	);
	

	echo json_encode($news);