[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/CodingJun/APIJSON/master/APIJSON-JavaScript/RequestUtil.js [Back]  [Original]

/**
 * Created by Lemon on 17/5/8.
 */
const TAG_REQUEST_UTIL = 'RequestUtil';

const URL_BASE = "http://39.108.143.172:8080"; // 
const URL_GET = URL_BASE + "/get"; // 
const URL_HEAD = URL_BASE + "/head"; // 
const URL_GETS = URL_BASE + "/gets"; // POSTGET
const URL_HEADS = URL_BASE + "/heads"; // POSTHEAD
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;
}

/**JSONString
 * @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
 */
function format(json) {
  try {
    return JSON.stringify(JSON.parse(json), null, "\t");
  } catch(e) {
    log(TAG_REQUEST_UTIL, 'format  try { ... } catch (err) { \n ' + e);
    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);
}

/**jsonJSON
 * @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() == '';
}


//>>>>>>>>>>>>>>>>>>

Web Proxy Viewer  |  New URL  |  Original Page