common_ajax.js 5.9 KB

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