FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GitHub Viewer

/** * Created by Lemon on 17/5/8. */ const TAG_REQUEST_UTIL = 'RequestUtil'; const URL_BASE = "http://139.196.140.118:8080"; // 基地址 const URL_GET = URL_BASE + "/get"; // 常规获取数据方式 const URL_HEAD = URL_BASE + "/head"; // 检查,默认是非空检查,返回数据总数 const URL_GETS = URL_BASE + "/gets"; // 通过POST来GET数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求 const URL_HEADS = URL_BASE + "/heads"; // 通过POST来HEAD数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求 const URL_POST = URL_BASE + "/post"; // 新增(或者说插入)数据 const URL_PUT = URL_BASE + "/put"; // 修改数据,只修改传入字段对应的值 const URL_DELETE = URL_BASE + "/delete"; // 删除数据 /**请求,全走HTTP POST * @param url * @param rq */ function request(url, json, notAlertRequest, onreadystatechange) { if (json == null || json instanceof Array || (json instanceof Object) == false) { alertOfDebug("request json == null || json instanceof Array || (json instanceof Object) == false !!! >> return null;"); return null; } if (url == null || (typeof url != "string")) { alertOfDebug("request url == null || typeof url != string !!! >> return null;"); return null; } if (url.length < 3 || url.indexOf(".") < 0) { alertOfDebug("request url.length < 3 || url.indexOf(.) < 0 !!! >> return null;"); return null; } var rqf = format(JSON.stringify(json)); var branch = url.substring(URL_BASE.length + 1, url.length); var end = branch.indexOf("/"); var method = branch.substring(0, end < 0 ? branch.length : end); var METHOD = method.toUpperCase(); if (! notAlertRequest) { alert("Request(" + METHOD + "):\n" + rqf); } var request = new XMLHttpRequest(); request.open("POST", url, true); request.setRequestHeader("Content-type", "application/json"); request.onreadystatechange = onreadystatechange != null ? onreadystatechange : function () { if (request.readyState !== 4) { return; } if (request.status === 200) { alert("Response(" + METHOD + "):\n" + format(request.responseText)); } else { alert("Response(" + METHOD + "):\nstatus" + request.status + "\nerror:" + request.error); } } request.send(rqf); //原生请求>>>>>>>>>>>>>>>>>>>>>>>>>> //JQuery ajax请求> //VUE axios请求> return request; } /**编码JSON,转义所有String * @param json 任意类型 */ function encode(json) { // alertOfDebug("encode before:\n" + format(JSON.stringify(json))); if (typeof json == "string") { //json instanceof String) { json = encodeURIComponent(json); } else if (json instanceof Array) { // alertOfDebug("encode json instanceof Array"); for (var i = 0; i < json.length; i ++) { // alertOfDebug("json[" + i + "] = " + format(JSON.stringify(json[i]))); json[i] = encode(json[i]); } } else if (json instanceof Object) { // alertOfDebug("encode json instanceof Object"); for (var key in json) { // alertOfDebug("encode json[" + key + "] = " + format(JSON.stringify(json[key]))); json[key] = encode(json[key]); } } // alertOfDebug("encode after:\n" + format(JSON.stringify(json))); return json; } /**格式化JSON串 * @param json {},JSON对象 */ function format(json) { if ((json instanceof Object) == false) { alertOfDebug("format json instanceof Object == false >> json = parseJSON(json);"); json = parseJSON(json); } return JSON.stringify(json, null, "\t"); } /**将json字符串转为JSON对象 * @param s */ function parseJSON(s) { if (s instanceof Object) { alertOfDebug("parseJSON s instanceof JSON >> return s;"); return s; } if (typeof s != "string") { alertOfDebug("parseJSON typeof json != string >> s = \"\" + s;"); s = "" + s; } // alertOfDebug("parseJSON s = \n" + s); return JSON.parse(s); } /**测试用的提示 * @param s */ function alertOfDebug(s) { // alert(s); //注释掉就都不会弹窗了 } /**是否为空 * @param s * @returns {boolean} */ function isEmpty(s) { return s == null || s.trim() == ''; } /**转为JSON对象 * @param s * @return {*} */ function parseObject(s) { if (s == null || s instanceof Object) { return s; } if (typeof s == 'string') { if (s.indexOf("'") >= 0) { s = s.replace(/'/g, "\'"); } return JSON.parse(s); } return parseObject(s.toString()); } // /**格式化JSON串 // * @param json // */ // function format(json) { // try { // return JSON.stringify(JSON.parse(json), null, "\t"); // } catch(e) { // log(TAG_REQUEST_UTIL, 'format try { ... } catch (err) { \n ' + err); // return json; // } // // // 导致格式化后代码很难看,像没格式化一样 // // if (json == null || json == '') { // // console.log('format json == null || json == "" >> return json;'); // // return json; // // } // // // // if (json instanceof Object) { //避免赋值影响传进来的json // // return JSON.stringify(json, null, "\t"); // // } // // // // var jsonObj; // // if (typeof json == 'string'){ // // try { // // jsonObj = JSON.parse(json); // // } catch (err) { // // console.log('format try { jsonObj = JSON.parse(json); } catch (err) { \n ' + err); // // return json; // // } // // } // // else { // // console.log('format json type error !'); // // return json; // // } // // return JSON.stringify(jsonObj, null, "\t"); // } function log(tag, msg) { console.log(tag + '.' + msg); } //常用请求>>>>>>>>>>>>>>>>>>

Back | FazBrowse Home | New Git URL