/** * 基本服务器交互类 */ var ServiceOperation = { // BaseUrl: "http://10.10.0.69:10022/FBS_MES/", BaseUrl: "http://127.0.0.1:8088/FBS_MES/", // BaseUrl: "http://103.40.192.17:8086/FBS_MES/", // BaseUrl: "http://106.15.206.14:8087/FBS_MES/", MethodPost: "POST", // post请求 MethodPut: "PUT", // put请求 MethodGet: "GET", // get请求 MethodDelete: "DELETE", // delete请求 /** * 获取完整的请求路径 */ getFullUrl: function(url) { return this.BaseUrl + url; }, /** * 发送请求 */ send: function(method, url, params, isAsync, callback, isMask, jsonRequest) { jQuery.support.cors = true; var strParams = ""; var contentType = ""; if (jsonRequest) { // strParams = JSON.stringify(params); // if (strParams == "{}") strParams = ""; strParams = params; contentType = "application/json;charset=utf-8"; } else { strParams = params; } if (isMask) top.layer.msg( "正在执行,请稍候……", { icon: 16, shade: 0.5 }, function() { $.ajax({ contentType: contentType, type: method, url: url, data: strParams, dataType: "json", async: isAsync, success: function(data) { if (callback != undefined) callback(data); }, complete: function(XMLHttpRequest, textStatus) {}, error: function(XMLHttpRequest, textStatus) { ServiceOperation.onload(XMLHttpRequest); }, beforeSend: function(XMLHttpRequest) {}, headers: { "X-Access-Token": SessionOperation.getToken() } }); } ); else $.ajax({ contentType: contentType, type: method, url: url, data: strParams, dataType: "json", async: isAsync, success: function(data) { if (callback != undefined) callback(data); }, complete: function(XMLHttpRequest, textStatus) {}, error: function(XMLHttpRequest, textStatus) { console.log(XMLHttpRequest); ServiceOperation.onload(XMLHttpRequest); }, beforeSend: function(XMLHttpRequest) {}, headers: { "X-Access-Token": SessionOperation.getToken() } }); }, /** * 对返回错误的结果进行预处理 */ onload: function(resp) { var status = resp.status; switch (status) { case 200: return resp; case 401: console.warn("未登录或登录已过期,请重新登录!"); window.top.location.replace("/login.html"); break; case 403: case 404: case 500: console.log(resp.responseJSON.message); if (resp.responseJSON.message === "Token失效,请重新登录") { console.warn("未登录或登录已过期,请重新登录。"); // 清空 token 信息 window.sessionStorage.clear(); window.top.location.replace("/login.html"); } else { // window.top.location.replace("/exception/" + status + ".html"); } break; default: console.warn( "未可知错误,大部分是由于后端不支持CORS或无效配置引起", resp ); return resp; } } };