PanDuWeb/js/request.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-07-20 09:02:14 +08:00
const baseURL = "http://192.168.10.102:9008";
// const baseURL = "http://175.27.168.120:6019";
2024-07-24 08:50:23 +08:00
// const imageURL = "http://175.27.168.120:6023/";
const imageURL = "http://192.168.10.102:9011/";
2024-07-17 15:42:30 +08:00
const timeout = 3000;
// requet ajax
2024-07-17 17:28:54 +08:00
function request(url, method, data = {}, contentType, back) {
$.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);
2024-07-24 08:50:23 +08:00
if (error.responseJSON && error.responseJSON.code == 401) {
2024-07-18 17:18:42 +08:00
toastr.error("登录信息已过期,请重新登录");
localStorage.removeItem("token");
window.location.href = "login.html";
return false;
} else {
return typeof back == "function" && back(null);
}
2024-07-17 17:28:54 +08:00
},
});
}
function getAjaxRequst(url, _data, callBack) {
request(url, "GET", _data, "application/json", function (res) {
2024-07-18 17:18:42 +08:00
if (res && res.code == 200) {
return typeof callBack == "function" && callBack(res);
} else {
2024-07-24 08:50:23 +08:00
toastr.error("网络错误");
2024-07-18 17:18:42 +08:00
}
2024-07-17 17:28:54 +08:00
});
}
function postAjaxRequst(url, _data, callBack) {
request(url, "POST", _data, "application/json", function (res) {
return typeof callBack == "function" && callBack(res);
});
}
2024-07-24 08:50:23 +08:00
function uploadFile(url, data, callBack) {
$.ajax({
url: imageURL + url,
data: data,
method: "POST",
processData: false,
contentType: false,
success: function (data) {
return typeof callBack == "function" && callBack(data);
},
error: function (error) {
console.log(error);
return typeof callBack == "function" && callBack(null);
},
});
}