Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 46

ligenhui21/APIJSON-Demo

forked from APIJSON/APIJSON-Demo
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (31)
master
6.0.0
5.4.0
5.3.0
5.2.5
5.2.0
5.1.5
5.1.0
5.0.0
4.9.1
4.8.3
4.8.0
4.7.3
4.7.2
4.7.1
4.7.0
4.6.7
4.6.6
4.6.5
4.6.3
4.6.0
master
Branches (1)
Tags (31)
master
6.0.0
5.4.0
5.3.0
5.2.5
5.2.0
5.1.5
5.1.0
5.0.0
4.9.1
4.8.3
4.8.0
4.7.3
4.7.2
4.7.1
4.7.0
4.6.7
4.6.6
4.6.5
4.6.3
4.6.0
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (31)
master
6.0.0
5.4.0
5.3.0
5.2.5
5.2.0
5.1.5
5.1.0
5.0.0
4.9.1
4.8.3
4.8.0
4.7.3
4.7.2
4.7.1
4.7.0
4.6.7
4.6.6
4.6.5
4.6.3
4.6.0
RequestUtil.js 12.18 KB
Copy Edit Raw Blame History
TommyLemon authored 2020年09月15日 21:59 +08:00 . 从 APIJSON 主项目迁移 Demo 项目过来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
/**
* Created by Lemon on 17/5/8.
*/
const TAG_REQUEST_UTIL = 'RequestUtil';
const URL_BASE = "http://apijson.cn: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请求<<<<<<<<<<<<<<<<<<<<<<<<<<
// $.ajax({
// type: isGet ? "GET" : "POST",
// url: isGet ? url + "/" + rq : url,
// contentType: "application/json", //必须
// dataType: "json", //返回值类型,非必须
// data: isGet ? null : rq,
// success: function (response) {
// alertOfDebug(response);
// }
// });
//JQuery ajax请求>>>>>>>>>>>>>>>>>>>>>>>>>>
//VUE axios请求<<<<<<<<<<<<<<<<<<<<<<<<<<
// if (isGet) {
// axios.get(url + "/" + rq, null)
// .then(function (response) {
// console.log(response);
// })
// .catch(function (error) {
// console.log(error);
// });
// } else {
// axios({
// method: 'post',
// url: url + "/",
// data: json
// }).then(function (response) {
// alertOfDebug(response);
// }).catch(function (error) {
// alertOfDebug(error);
// });
// }
//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
*/
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);
}
/**将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() == '';
}
//常用请求<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取单个对象
* @param table String, 对象名,如 "User"
* @param id Long, 对象id,如 1
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function getObject(table, id, notAlert, callBack) {
alertOfDebug("getObject table = " + table + "; id = " + id);
return request(URL_GET, newSingleJSON(table, { "id": id }, null), notAlert, callBack);
}
/**获取数组
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param count int, 每页数量
* @param page int, 页码
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function getArray(table, json, count, page, notAlert, callBack) {
alertOfDebug("getArray table = " + table + "; count = " + count + "; page = " + page
+ "; json = \n" + format(json));
return request(URL_GET, newArrayJSON(table, json, count, page), notAlert, callBack);
}
/**新增单个对象
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function postObject(table, json, notAlert, callBack) {
alertOfDebug("postObject table = " + table
+ "; json = \n" + format(json));
var id = json == null ? null : json['id'];
if (id != null) {
alertOfDebug('postObject POST 请求不能设置 id !');
return;
}
return request(URL_POST, newSingleJSON(table, json, table), notAlert, callBack);
}
/**修改单个对象
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function putObject(table, json, notAlert, callBack) {
alertOfDebug("putObject table = " + table
+ "; json = \n" + format(json));
if (json == null) {
alertOfDebug('putObject PUT 请求必须设置 table对象 !');
return;
}
if (json["id"] == null || json["id"] <= 0) {
alertOfDebug('putObject PUT 请求必须设置 id 且 id > 0 !');
return;
}
return request(URL_PUT, newSingleJSON(table, json, table), notAlert, callBack);
}
/**删除单个对象
* @param table String, 对象名,如 "User"
* @param id Long, 对象id,如 1
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function deleteObject(table, id, notAlert, callBack) {
alertOfDebug("deleteObject table = " + table + "; id = " + id);
if (id == null || id <= 0) {
alertOfDebug('deleteObject DELETE 请求必须设置 id 且 id > 0 !');
return;
}
return request(URL_DELETE, newSingleJSON(table, { "id": id }, table), notAlert, callBack);
}
/**修改多个对象
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function putArray(table, json, notAlert, callBack) {
alertOfDebug("putArray table = " + table
+ "; json = \n" + format(json));
var idArray = json == null ? null : json["id{}"];
if (idArray == null || (idArray instanceof Array) == false) {
alertOfDebug('putArray idArray == null || (idArray instanceof Array) == false' +
' >> return; PUT 请求必须设置 id{}:[] !');
return;
}
return request(URL_PUT, newSingleJSON(table, json, table + "[]"), notAlert, callBack);
}
/**删除多个对象
* @param table String, 对象名,如 "User"
* @param idArray [], 对象id数组,如 [1, 2, 3]
* @param notAlert 不弹窗显示
* @param callBack 请求成功回调
*/
function deleteArray(table, idArray, notAlert, callBack) {
alertOfDebug("deleteArray table = " + table
+ "; idArray = \n" + format(idArray));
if (idArray == null || (idArray instanceof Array) == false) {
alertOfDebug('deleteArray idArray == null ! DELETE 请求必须设置 id{}:[] !');
return;
}
return request(URL_DELETE, newSingleJSON(table, { "id{}": idArray }, table + "[]"), notAlert, callBack);
}
/**新建单个对象请求
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param tag String, 写操作标识,一般来说,操作单个对象时和table相同,操作多个对象时是 table[]
*/
function newSingleJSON(table, json, tag) {
// alertOfDebug("newSingleJSON table = " + table + "; tag = " + tag
// + "; json = \n" + format(json));
return parseJSON(newSingleString(table, json, tag));
}
/**新建数组请求
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param count int, 每页数量
* @param page int, 页码
*/
function newArrayJSON(table, json, count, page) {
// alertOfDebug("newArrayJSON table = " + table + "; count = " + count + "; page = " + page
// + "; json = \n" + format(json));
return parseJSON(newArrayString(table, json, count, page));
}
/**新建单个对象请求
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param tag String, 写操作标识,一般来说,操作单个对象时和table相同,操作多个对象时是 table[]
*/
function newSingleString(table, json, tag) {
if (json == null) {
alertOfDebug('newSingleString json == null >> return;');
return;
}
// alertOfDebug("newSingleString table = " + table + "; tag = " + tag
// + "; json = \n" + format(json));
return "{\""
+ table + "\":"
+ JSON.stringify(json)
+ (isEmpty(tag) ? "" : ",\"tag\":\"" + tag + "\"")
+ "}";
}
/**新建数组请求
* @param table String, 对象名,如 "User"
* @param json {}, 对象内容,如 {"sex":1}
* @param count int, 每页数量
* @param page int, 页码
*/
function newArrayString(table, json, count, page) {
if (json == null) {
alertOfDebug('newArrayString json == null >> return;');
return;
}
// alertOfDebug("newArrayString table = " + table + "; count = " + count + "; page = " + page
// + "; json = \n" + format(json));
return "{\"" + table + "[]\":{" + "\"count\":" + count + "," + "\"page\":" + page + ",\""
+ table + "\":" + JSON.stringify(json) + "}}";
}
//常用请求>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

腾讯 APIJSON 各种语言、各种框架 的 使用示例项目、上手文档、测试数据 SQL 文件 等。原仓库见
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ligenhui21/APIJSON-Demo.git
git@gitee.com:ligenhui21/APIJSON-Demo.git
ligenhui21
APIJSON-Demo
APIJSON-Demo
master
Going to Help Center

Search

Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /