class Utils { /** * 发送Get请求 * * @param {!string} url 请求地址 * @param {?function} next 回调函数 */ static ajaxGet(url, next) { let xhr = new XMLHttpRequest(); if (url.includes('?')) { // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存 xhr.open('GET', `${url}&date=${new Date().getTime()}`, true); } else { // 添加一个date参数加入当前时间以避免缓存 xhr.open('GET', `${url}&date=${new Date().getTime()}`, true); } xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies xhr.withCredentials = true; xhr.responseType = 'json'; xhr.onload = function (res) { // 获取请求接口返回值 let response = res.target.response; next && next(response); }; xhr.send(); } /** * 发送Post请求 * * @param {!string} url 请求地址 * @param {?string} data post请求参数 * @param {?function} next 回调函数 */ static ajaxPost(url, data, next) { let xhr = new XMLHttpRequest(); if (url.includes('?')) { // 在URL有其他参数时,添加一个date参数加入当前时间以避免缓存 xhr.open("POST", `${url}&${new Date().getTime()}`, true); } else { // 添加一个date参数加入当前时间以避免缓存 xhr.open("POST", `${url}?${new Date().getTime()}`, true); } xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // 更改XMLHttpRequest对象withCredentials属性以支持跨域Cookies xhr.withCredentials = true; xhr.responseType = 'json'; xhr.onload = function (res) { // 获取请求接口返回值 let response = res.target.response; next && next(response); }; xhr.send(data); } } window.onload = function () { let signinButton = document.querySelector('#signin-button') signinButton.addEventListener('click', () => { let userId = document.querySelector('body > div.content > div.content1 > ul > li:nth-child(1) > input').value let password = document.querySelector('body > div.content > div.content1 > ul > li:nth-child(2) > input').value let cCode = document.querySelector('body > div.content > div.content1 > ul > li:nth-child(3) > input').value let signInUrl = 'http://admin.cloudsql.1473.cn/v1/signin' let postData = `userId=${userId}&password=${password}&captcha=${cCode}` Utils.ajaxPost(signInUrl, postData, res => { if (res.status == 'signedin') { //跳转到首页 window.location.href = 'index.html' } else { alert('登陆失败') } }) }) }