注册html代码,判断用户名是否可用,用get请求
Document
登录
Document
首页,点击登录按钮后,跳转到首页,如果未登录成功则跳出div内内容,用户需点击登录按钮跳转到登录页面重新登陆
Document
亲,请 登录
欢迎 xxx
php
成功
} else {
echo '0'; # 0 --->失败
}
} else {
echo 'connect error';
}
?>
封装的js文件
function ajaxtool(options) { //options--->对象
let xhr = new XMLHttpRequest();
if (options.type.toLowerCase() == 'get') {
xhr.open("get", options.url + "?" + options.data);//携带请求参数到后端
xhr.send()
}
if (options.type.toLowerCase() == 'post') {
xhr.open("post", options.url);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
// post请求需要把请求参数写在 send中,但是不能带"""
xhr.send(options.data);
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
options.success(xhr.responseText);
}
}
}
// 获取指定键名的cookie值得
function getCookie(key) {
let cookie = document.cookie; //'username=zhangsan; password=123456'
var arr = cookie.split("; "); // ["username=zhangsan","password=123456",...]
for (let i = 0; i < arr.length; i++) {
// arr[i] --- > "username=zhangsan" 或者 "password=123456"
let item = arr[i].split("="); // item[0] -->键 item[1]---》值
if (item[0] == key) {
return item[1]; // 返回结果, 终止函数
}
}
return "";
}
// 设置cookie的
function setCookie(key, value, day) {
// let date = new Date(); //当前本地时间
// date.setDate(date.getDate() + day); // 修改日期对象
// document.cookie = key + '=' + value + ';expires=' + date;
let d = new Date();
d.setDate(d.getDate() + day); // 修改过期时间 d是一个时间对象,7天后的日期对象
document.cookie = key+'='+value+';expires=' + d;
}
// 删除cookie的
function delCookie(key) {
setCookie(key, "", -1);
}
function $(selector) {
return document.querySelector(selector);
}
暑期编程PK赛
得CSDN机械键盘等精美礼品!


