Skip to content

Commit

Permalink
新增JavaScript版
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Jun 18, 2017
1 parent 71d0978 commit e76770e
Show file tree
Hide file tree
Showing 17 changed files with 975 additions and 0 deletions.
12 changes: 12 additions & 0 deletions APIJSON-JavaScript/.idea/Demo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions APIJSON-JavaScript/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

322 changes: 322 additions & 0 deletions APIJSON-JavaScript/.idea/workspace.xml

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions APIJSON-JavaScript/access_error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Access Error</title>
<script type="text/javascript" src="apijson.js">
</script>
</head>
<body>
<script >
var json = {
"tag":"Wallet",
"Wallet":{
"userId":38710
}
};

request(url_post_get, json);
</script>
</body>
</html>
22 changes: 22 additions & 0 deletions APIJSON-JavaScript/access_permitted.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<title>Access Permitted</title>
<script type="text/javascript" src="apijson.js">
</script>
</head>
<body>
<script >
var json = {
"tag":"Wallet",
"Wallet":{
"userId":38710
},
"currentUserId":38710,
"loginPassword":"apijson"
};

request(url_post_get, json);
</script>
</body>
</html>
194 changes: 194 additions & 0 deletions APIJSON-JavaScript/apijson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/**
* Created by Lemon on 17/5/8.
*/

var url_base = "http://139.196.140.118:8080";
var url_get = url_base + "/get";
var url_head = url_base + "/head";
var url_post_get = url_base + "/post_get";
var url_post_head = url_base + "/post_head";
var url_post = url_base + "/post";
var url_put = url_base + "/put";
var url_delete = url_base + "/delete";


/**包含
* @param obj
* @returns {boolean}
*/
Array.prototype.contains = function (obj) {
for (var i = 0; i < this.length; i ++) {
if (this[i] === obj) {
return true;
}
}
return false;
}

/**请求方法
* @type {Array}
*/
var Method = new Array(

/**
* 常规获取数据方式
*/
"get",

/**
* 检查,默认是非空检查,返回数据总数
*/
"head",

/**
* 通过POST来GET数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
*/
"post_get",

/**
* 通过POST来HEAD数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
*/
"post_head",

/**
* 新增(或者说插入)数据
*/
"post",

/**
* 修改数据,只修改传入字段对应的值
*/
"put",

/**
* 删除数据
*/
"delete"
)



/**请求
* @param url
* @param rq
*/
function request(url, json, notAlertRequest, onreadystatechange) {
var rqf = format(JSON.stringify(json));

var rq = encodeURI(JSON.stringify(encode(json))); // JSON.stringify(encode(json)); //


var method = url.substring(url.lastIndexOf("/") + 1, url.length);
// alert("method=" + method);
if (method == null || Method.contains(method) == false) {
alert("method is empty! url must endsWith \"/[method]\" !");
return;
}
var isGet = method === Method[0] || method === Method[1];
// alert("isGet=" + isGet);

var METHOD = method.toUpperCase();

if (! notAlertRequest) {
alert("Request(" + METHOD + "):\n" + rqf);
}


//原生请求<<<<<<<<<<<<<<<<<<<<<<<<<<
var request = new XMLHttpRequest();
request.open(isGet ? "GET" : "POST", url + (isGet ? "/" + rq : ""), true);
if (isGet == false) {
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(isGet ? null : rq);
//原生请求>>>>>>>>>>>>>>>>>>>>>>>>>>


//JQuery ajax请求<<<<<<<<<<<<<<<<<<<<<<<<<<
// $.ajax({
// type: isGet ? "GET" : "POST",
// url: isGet ? url + "/" + rq : url,
// contentType: "application/json", //必须
// dataType: "json", //返回值类型,非必须
// data: isGet ? null : rq,
// success: function (response) {
// alert(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) {
// alert(response);
// }).catch(function (error) {
// alert(error);
// });
// }
//VUE axios请求>>>>>>>>>>>>>>>>>>>>>>>>>>


return request;
}

/**编码JSON,转义所有String
* @param json
*/
function encode(json) {
// alert("encode before:\n" + format(JSON.stringify(json)));

if (typeof json == "string") { //json instanceof String) {
json = encodeURIComponent(json);
}
else if (json instanceof Array) {
// alert("encode json instanceof Array");

for (var i = 0; i < json.length; i ++) {
// alert("json[" + i + "] = " + format(JSON.stringify(json[i])));
json[i] = encode(json[i]);
}
}
else if (json instanceof Object) {
// alert("encode json instanceof Object");
for (var key in json) {
// alert("encode json[" + key + "] = " + format(JSON.stringify(json[key])));
json[key] = encode(json[key]);
}
}
// alert("encode after:\n" + format(JSON.stringify(json)));

return json;
}


/**格式化JSON串
* @param json
*/
function format(json) {
return JSON.stringify(JSON.parse(json), null, "\t");
}
20 changes: 20 additions & 0 deletions APIJSON-JavaScript/delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>DELETE</title>
<script type="text/javascript" src="apijson.js">
</script>
</head>
<body>
<script >
var json = {
"tag":"Moment",
"Moment":{
"id":10000
}
};

request(url_delete, json);
</script>
</body>
</html>
12 changes: 12 additions & 0 deletions APIJSON-JavaScript/get/.idea/GET.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions APIJSON-JavaScript/get/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e76770e

Please sign in to comment.