common_ajax.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Created by Mai on 2017/4/20.
  3. */
  4. var CommonAjax = {
  5. get:function (url,data,cb,dataType) {
  6. $.get(url,data,cb,dataType)
  7. },
  8. post: function (url, data, successCallback, errorCallback) {
  9. $.ajax({
  10. type:"POST",
  11. url: url,
  12. data: {'data': JSON.stringify(data)},
  13. dataType: 'json',
  14. cache: false,
  15. timeout: 50000,
  16. success: function(result){
  17. if (result.error === 0) {
  18. if (successCallback) {
  19. successCallback(result.data);
  20. }
  21. } else {
  22. alert('error: ' + result.message);
  23. if (errorCallback) {
  24. errorCallback();
  25. }
  26. }
  27. },
  28. error: function(jqXHR, textStatus, errorThrown){
  29. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  30. if (errorCallback) {
  31. errorCallback();
  32. }
  33. }
  34. });
  35. },
  36. compressPost: function (url, data, successCallback, errorCallback) {
  37. $.ajax({
  38. type:"POST",
  39. url: url,
  40. data: {'data': LZString.compress(JSON.stringify(data))},
  41. dataType: 'json',
  42. cache: false,
  43. timeout: 50000,
  44. success: function(result){
  45. if (result.error === 0) {
  46. if (successCallback) {
  47. successCallback(result.data);
  48. }
  49. } else {
  50. alert('error: ' + result.message);
  51. if (errorCallback) {
  52. errorCallback();
  53. }
  54. }
  55. },
  56. error: function(jqXHR, textStatus, errorThrown){
  57. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  58. if (errorCallback) {
  59. errorCallback();
  60. }
  61. }
  62. });
  63. },
  64. postRationLib: function (url, data, successCallback, errorCallback) {
  65. $.ajax({
  66. type:"POST",
  67. url: url,
  68. data: data,
  69. dataType: 'json',
  70. cache: false,
  71. timeout: 50000,
  72. success: function(result){
  73. if (!result.error) {
  74. if (successCallback) {
  75. successCallback(result.data);
  76. }
  77. } else {
  78. alert('error: ' + result.message);
  79. if (errorCallback) {
  80. errorCallback();
  81. }
  82. }
  83. },
  84. error: function(jqXHR, textStatus, errorThrown){
  85. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  86. if (errorCallback) {
  87. errorCallback();
  88. }
  89. }
  90. });
  91. },
  92. postEx: function(url, params, dftTimeOutMilSec, isAsync, successCallback, failCallback, exceptionCallback){
  93. $.ajax({
  94. type:"POST",
  95. url: url,
  96. data: {'params': JSON.stringify(params)},
  97. dataType: 'json',
  98. cache: false,
  99. async: isAsync,
  100. timeout: dftTimeOutMilSec,
  101. success: function(result){
  102. if (!result.error) {
  103. if (successCallback) {
  104. successCallback(result.data);
  105. }
  106. } else {
  107. alert('error: ' + result.message);
  108. if (failCallback) {
  109. failCallback();
  110. }
  111. }
  112. },
  113. error: function(jqXHR, textStatus, errorThrown){
  114. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  115. if (exceptionCallback) {
  116. exceptionCallback();
  117. }
  118. }
  119. });
  120. },
  121. specialPost:function (url, data,successCallback, errorCallback) {
  122. $.ajax({
  123. url: url,
  124. type: 'post',
  125. data: data,
  126. dataType: 'json',
  127. error: function(jqXHR, textStatus, errorThrown) {
  128. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  129. if (errorCallback) {
  130. errorCallback();
  131. }
  132. },
  133. success: function(response) {
  134. // 修改失败则恢复原值
  135. if (response.err !== 0) {
  136. if (errorCallback) {
  137. errorCallback(response);
  138. }
  139. } else {
  140. if (successCallback) {
  141. successCallback(response);
  142. }
  143. }
  144. }
  145. });
  146. }
  147. };
  148. /**
  149. * 设置全局的AJAX请求默认选项
  150. * 主要设置了AJAX请求遇到Session过期的情况
  151. */
  152. $.ajaxSetup({
  153. complete: function (data) {
  154. if (data.responseJSON&&data.responseJSON.ret_code && data.responseJSON.ret_code == 99) {
  155. alert(data.responseJSON.ret_msg);
  156. var top = getTopWindow();
  157. setTimeout('top.location.href = "/login";', 300);
  158. }
  159. }
  160. });
  161. async function ajaxPost(url, data, isPlainData = false) {
  162. return new Promise(function (resolve, reject) {
  163. $.ajax({
  164. type:"POST",
  165. url: url,
  166. data: isPlainData ? data : {'data': JSON.stringify(data)},
  167. dataType: 'json',
  168. cache: false,
  169. timeout: 200000,
  170. success: function(result){
  171. if (!result.error || commonUtil.isDef(result.err) && !result.err) {
  172. resolve(result.data);
  173. } else {
  174. const message = result.message || result.msg;
  175. alert('error: ' + message);
  176. reject(message);
  177. }
  178. },
  179. error: function(jqXHR, textStatus, errorThrown){
  180. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  181. reject("请求错误");
  182. }
  183. });
  184. });
  185. }
  186. async function ajaxGet(url,data){
  187. return new Promise(function (resolve, reject) {
  188. $.get(url,data,function (result,status) {
  189. if(status == 'success'){
  190. resolve(result);
  191. }else {
  192. reject(result);
  193. }
  194. },"json")
  195. })
  196. }
  197. /**
  198. * 在页面中任何嵌套层次的窗口中获取顶层窗口
  199. * @return 当前页面的顶层窗口对象
  200. */
  201. function getTopWindow() {
  202. var p = window;
  203. while (p != p.parent) {
  204. p = p.parent;
  205. }
  206. return p;
  207. }
  208. function ajaxErrorInfo(jqXHR, textStatus, errorThrown) {
  209. if(textStatus == 'timeout'){
  210. alert('网络连接超时,请刷新您的网页。');
  211. }else {
  212. alert('url: ' + url +' error ' + textStatus + " " + errorThrown);
  213. }
  214. }