登录校验显示
function fn() {
if( localStorage.token ==undefined){
alert('请先登录,获取Token')
return ;
}
let xhr = new XMLHttpRequest();
let url = 'timemeetyou.com:8889/api/private…';
xhr.open('get', url, true);
xhr.setRequestHeader("Authorization", localStorage.token)
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let res = JSON.parse(xhr.responseText)
console.log(res);
}
\
}
}
function login() {
let xhr = new XMLHttpRequest();
let url = 'timemeetyou.com:8889/api/private…';
xhr.open('post', url, true);
let params = {
username: "admin",
password: "123456"
}
/* post需要添加请求头 */
/* 请求回来的内容是json格式 */
/* Content-type 表示请求内容的类型 */
/* application/json 表示请求内容的类型的具体的值 */
xhr.setRequestHeader("Content-type", "application/json")
xhr.send(JSON.stringify(params));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let res = JSON.parse(xhr.responseText)
console.log(res);
/* log 出后台返回的结果 msg的内容 */
console.log(res.meta.msg);
localStorage.token = res.data.token;
}
}
}