PanDuWeb/js/request.js

51 lines
1.4 KiB
JavaScript

// const baseURL = "http://192.168.10.102:9008";
const baseURL = "http://175.27.168.120:6019";
const imageURL = "http://175.27.168.120:6023/";
const timeout = 3000;
// requet ajax
function request(url, method, data = {}, contentType, back) {
console.log("data::: ", data);
$.ajax({
url: baseURL + url,
type: method,
cache: false,
timeout: timeout,
contentType: contentType,
headers: {
"X-Token": localStorage.getItem("token") || "",
},
dataType: "json",
data: JSON.stringify(data),
success: function (res) {
return typeof back == "function" && back(res);
},
error: function (error) {
console.log(error);
if (error.responseJSON.code == 401) {
toastr.error("登录信息已过期,请重新登录");
localStorage.removeItem("token");
window.location.href = "login.html";
return false;
} else {
alert("网络错误");
return typeof back == "function" && back(null);
}
},
});
}
function getAjaxRequst(url, _data, callBack) {
request(url, "GET", _data, "application/json", function (res) {
if (res && res.code == 200) {
return typeof callBack == "function" && callBack(res);
} else {
toastr.error(res.message);
}
});
}
function postAjaxRequst(url, _data, callBack) {
request(url, "POST", _data, "application/json", function (res) {
return typeof callBack == "function" && callBack(res);
});
}