permission.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //ajax
  2. function AjaxRequest(requestURL) {
  3. //请求的URL
  4. var url = requestURL;
  5. /**
  6. 请求参数字符串
  7. */
  8. var queryString = "";
  9. /*
  10. (默认: true) 默认设置下,所有请求均为异步请求;如果需要发送同步请求,请将此选项设置为 false
  11. */
  12. var async = true;
  13. /*
  14. 设置浏览器缓存
  15. */
  16. var cache = false;
  17. /*
  18. 请求方式
  19. */
  20. var type = "get";
  21. /*
  22. 服务器返回的类型
  23. */
  24. var dataType = "json";
  25. /*
  26. 发送请求前可修改 XMLHttpRequest 对象的函数
  27. */
  28. var beforeSend = null;
  29. /*
  30. (默认: 自动判断 (xml 或 html)) 请求失败时调用函数
  31. */
  32. var error = null;
  33. /*
  34. 请求成功后回调函数。参数:服务器返回数据,数据格式
  35. */
  36. var success = null;
  37. /*
  38. 请求完成后回调函数 (请求成功或失败时均调用)
  39. */
  40. var complete = null;
  41. /**
  42. *************************************************
  43. */
  44. this.setQueryString = function (qs) {
  45. queryString = qs;
  46. }
  47. this.getQueryString = function () {
  48. return queryString;
  49. }
  50. this.setUsePOST = function () {
  51. type = "POST";
  52. }
  53. this.setUseGET = function () {
  54. type = "GET";
  55. }
  56. this.setAsync = function (asyncBoolean) {
  57. async = asyncBoolean;
  58. }
  59. this.setBeforeSend = function (func) {
  60. beforeSend = func;
  61. }
  62. this.setError = function (func) {
  63. error = func;
  64. }
  65. this.setSuccess = function (func) {
  66. success = func;
  67. }
  68. this.setComplete = function (func) {
  69. complete = func;
  70. }
  71. /**
  72. send a Ajax Request
  73. */
  74. this.sendRequest = function () {
  75. var bl = (url.indexOf("?") == -1 ? false : true);
  76. $.ajax({
  77. type: type,
  78. url: url + (bl ? "&" : "?") + "ts=" + Math.round(Math.random() * 100),
  79. data: queryString,
  80. dataType: dataType,
  81. beforeSend: beforeSend,
  82. //contentType: "application/json",
  83. error: error,
  84. success: success,
  85. complete: complete
  86. });
  87. }
  88. }
  89. /*
  90. pUrl-访问URL
  91. pSuccess-成功后运行函数
  92. pError-失败后运行函数
  93. showPg-是否显示加载
  94. msg-加载提示
  95. type-同步还是异步
  96. */
  97. function getPermissionAjax(pUrl, pSuccess, pError, showPg, msg, type) {
  98. if (showPg) { progressShow(msg); }
  99. var errorFunc = null;
  100. var successFunc = null;
  101. errorFunc = function (data, status, error) {
  102. if (showPg) progressHide();
  103. alert("服务器返回错误!");
  104. };
  105. successFunc = function (data) {
  106. if (data == null) return;
  107. //显示进度
  108. if (showPg) progressHide();
  109. switch (data.type) {
  110. case "login":
  111. {
  112. loginIn();
  113. break;
  114. }
  115. case "success":
  116. {
  117. pSuccess(data.result);
  118. break;
  119. }
  120. case "error":
  121. {
  122. if (!pError || pError == null) {
  123. alert(data.result);
  124. } else {
  125. pError(data.result);
  126. }
  127. break;
  128. }
  129. default:
  130. {
  131. alert(data.result);
  132. break;
  133. }
  134. }
  135. };
  136. var ajax = new AjaxRequest(pUrl);
  137. if (type != "sync") ajax.setAsync(false);
  138. ajax.setError(errorFunc);
  139. ajax.setSuccess(successFunc);
  140. ajax.sendRequest();
  141. }
  142. function getJsonAjaxSync(pUrl, pSuccess, pError, showPg, msg) {
  143. getPermissionAjax(pUrl, pSuccess, pError, showPg, msg, "sync");
  144. }
  145. function getJsonAjax(pUrl, pSuccess, pError, showPg, msg) {
  146. getPermissionAjax(pUrl, pSuccess, pError, showPg, msg, "");
  147. }
  148. function postPermissionAjax(pUrl, pParm, pSuccess, pError, showPg, msg) {
  149. if (showPg) progressShow(msg);
  150. var ajax = new AjaxRequest(pUrl);
  151. var errorFunc = null;
  152. var successFunc = null;
  153. if (!pError) {
  154. errorFunc = function (data) {
  155. if (showPg) progressHide();
  156. alert("服务器返回错误!");
  157. }
  158. successFunc = function (data) {
  159. if (showPg) progressHide();
  160. switch (data.type) {
  161. case "login":
  162. {
  163. loginIn();
  164. break;
  165. }
  166. case "success":
  167. {
  168. pSuccess(data.result);
  169. break;
  170. }
  171. default:
  172. {
  173. alert(data.result);
  174. break;
  175. }
  176. }
  177. }
  178. } else {
  179. errorFunc = pError;
  180. successFunc = pSuccess;
  181. }
  182. ajax.setUsePOST();
  183. ajax.setError(errorFunc);
  184. ajax.setSuccess(successFunc);
  185. ajax.setQueryString(pParm);
  186. ajax.sendRequest();
  187. }