$.ajax({
type: 'GET',//请求类型
url: 'https://open.duyiedu.com/api/student/findAll',//请求地址
beforeSend: function (xhr, request) {//发出请求之前执行的函数,请求拦截
console.log(request.data);
},
data: {
page: 1
// appkey:'pppp_1569994479435'//请求参数
},
contentType: '',//编码类型
dataFilter: function (data, type) {//,响应拦截,返回拦截
},
dataType: 'json',//得到的数据类型
success: function (res) {//成功的回调函数
console.log(res);
},
error: function () {
}
});
/**
*
* @param {*} methods
* @param {*} url
* @param {*} data
* @param {*} callBack
*/
function ajax(method, url, data, callBack) {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
if (method == 'GET') {
xhr.open(method, url + '?' + data, true);
xhr.send();
} else {
xhr.open(method, url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(data);
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
callBack(JSON.parse(xhr.responseText));
}else{
console.log('error');
}
}
}
}