网页与服务器的三次握手
客户向服务器进行http请求
服务器讲数据传到客户
客户向服务器提交数据
实例:
<script type="text/javascript">
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("post", "http://www.qhdlink-student.top/student/banner.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = getbanner;
var postmsg = 'username=csgo & userpwd=12345678 & userclass=74 & type=2'
xmlHttp.send(postmsg);
function getbanner() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var con = xmlHttp.responseText;
var cons = eval("(" + con + ")");
var bannerc = document.getElementById("showbanner");
for (v in cons) {
var div = document.createElement("div")
var imgo = document.createElement("img");
imgo.src = "http://www.qhdlink-student.top/" + cons[v].path_banner;
div.appendChild(imgo);
bannerc.appendChild(div);
bannerc.children[v].setAttribute("class", "swiper-slide");
}
}
}
Ajxa封装:
function getData(fn, method, url, asy, data) {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.Activexobject) {
xmlHttp = new Activex0bject("Microsoft.XMLHTTP");
}
xm1Http.open(method, ur1, asy);
xm1Http.setRequestHeader("Content-type", "application/x-ww-fonm-urlencoded");
xm1Http.onreadystatechange = change;
if (data) {
xmlHttp.send(data);
} else i
xmlHttp.send();
function change() {
if (xm1Http.readystate == 4 & xm1Http.status == 200) {
var str = xmlHttp.responseText;
fn(str);
} else {
console.log("连接阶段");
}
}
}
调用:
document.form1.sub.onclick = function () {
var names = document.form1.username.value;
var pwd = document.form1.userpwd.value;
var cla = document.form1.userclass.value;
var t = document.form1.type.value;
var postmsg = "username=" + names + "&userpwd=" + pwd + "&userclass=" + cla + "&type=" + t;
getData(getLogin, "post", " http: // www.qhdlink-student.top/student/login.php", true, postmsg);
}
function getLogin(g) {
if (g == "ok") {
location.href = "index.html";
} else {
document.getElementById("showmsg").innerHTML = "用户名或密码错误";
}
}
function getA(str) {
console.log(str);
}
function sendData(fn) {
var a = 10;
fn(a);
}
sendData(getA);
