梦入琼楼寒有月,行过石树冻无烟

Ajax 发送服务器请求

在Ajax中,使用了XMLHttpRequest对象的open()、send()两个对象方法来事项将请求发送服务器,分别为GET方式和POST方式来提交请求,而两个请求的同一功能就是,都是提交数据:

1
2
xmlhttprequest.open("GET","Ajax_info.txt",ture);
xmlhttprequest.send();
ID DA FA
open(类型,url,异步) 类型:请求类型(GET或POST); url:文件在服务器上的位置;异步:true(异步)或false(同步) open
send(String) string:仅用于POST请求 send

GET / POST

GET

在GET方法之中,数据将会加载在URL中进行发送:

1
http://www.jiangxue.org.cn/login?one=test

POST

想对于GET方法来说,POST更加的安全和可靠,如通过Wireshark进行抓包可发现,GET只有一个数据传输,而POST方法会有两个数据传输,分别为Ajax发送和Server回应,相比之下他比GET更加安全,在URL内无法看见。

GET

xmlhttprequest.open(“GET”,”Ajax_info”,ture);

在GET方法之中,数据将会加载在URL中进行发送,DANG:

1
2
xmlhttprequest.open("GET","Ajax_info.txt",ture);
xmlhttprequest.send();
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ajax</title>
<script type="text/javascript">
function test() {
var xmlhttprequest_;
if (window.XMLHttpRequest) {
xmlhttprequest_ = new XMLHttpRequest();
} else {
xmlhttprequest_ = new ActoveXObject("Microsoft.XMLHTTP");
}
xmlhttprequest_.onreadystatechange = function() {
if (xmlhttprequest_.readyState == 4 && xmlhttprequest_.status == 200) {
document.getElementById("title").innerHTML=xmlhttprequest_.responseText;
}
}
xmlhttprequest_.open("GET","Hello.txt",true);
xmlhttprequest_.send();
}
</script>
</head>
<body>
<div id="title"><p>Hello,world!</p></div>
<button type="button" onclick="test()">up</button>
</body>
</html>

xmlhttprequest.open(“GET”,”Ajax_info?usernae=Sun&li”,true);

1
2
xmlhttprequest.open("GET","Ajax_info?usernae=Sun&li",true);
xmlhttprequest.send();

POST

xmlhttprequest.open(“POST”,”Ajax_info”,ture);

1
2
xmlhttprequest.open("POST","Ajax_info.txt",ture);
xmlhttprequest.send();

xmlhttprequest.open(“POST”,”ActoveXObject”,true);

1
2
3
xmlhttprequest.open("POST","ActoveXObject",true);
xmlhttprequest.setRequestHeader("Content-type","application/x-www-form-urkebcided");
xmlhttprequest.send("uname=chenheng&upwd=1213124");
⬅️ Go back