common_ajax.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. postRationLib: function (url, data, successCallback, errorCallback) {
  37. $.ajax({
  38. type:"POST",
  39. url: url,
  40. data: data,
  41. dataType: 'json',
  42. cache: false,
  43. timeout: 50000,
  44. success: function(result){
  45. if (!result.error) {
  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. postEx: function(url, params, dftTimeOutMilSec, isAsync, successCallback, failCallback, exceptionCallback){
  65. $.ajax({
  66. type:"POST",
  67. url: url,
  68. data: {'params': JSON.stringify(params)},
  69. dataType: 'json',
  70. cache: false,
  71. async: isAsync,
  72. timeout: dftTimeOutMilSec,
  73. success: function(result){
  74. if (!result.error) {
  75. if (successCallback) {
  76. successCallback(result.data);
  77. }
  78. } else {
  79. alert('error: ' + result.message);
  80. if (failCallback) {
  81. failCallback();
  82. }
  83. }
  84. },
  85. error: function(jqXHR, textStatus, errorThrown){
  86. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  87. if (exceptionCallback) {
  88. exceptionCallback();
  89. }
  90. }
  91. });
  92. },
  93. specialPost:function (url, data,successCallback, errorCallback) {
  94. $.ajax({
  95. url: url,
  96. type: 'post',
  97. data: data,
  98. dataType: 'json',
  99. error: function(jqXHR, textStatus, errorThrown) {
  100. ajaxErrorInfo(jqXHR, textStatus, errorThrown);
  101. if (errorCallback) {
  102. errorCallback();
  103. }
  104. },
  105. success: function(response) {
  106. // 修改失败则恢复原值
  107. if (response.err !== 0) {
  108. if (errorCallback) {
  109. errorCallback(response);
  110. }
  111. } else {
  112. if (successCallback) {
  113. successCallback(response);
  114. }
  115. }
  116. }
  117. });
  118. }
  119. };
  120. /**
  121. * 设置全局的AJAX请求默认选项
  122. * 主要设置了AJAX请求遇到Session过期的情况
  123. */
  124. $.ajaxSetup({
  125. complete: function (data) {
  126. if (data.responseJSON&&data.responseJSON.ret_code && data.responseJSON.ret_code == 99) {
  127. alert(data.responseJSON.ret_msg);
  128. var top = getTopWindow();
  129. setTimeout('top.location.href = "/login";', 300);
  130. }
  131. }
  132. });
  133. /**
  134. * 在页面中任何嵌套层次的窗口中获取顶层窗口
  135. * @return 当前页面的顶层窗口对象
  136. */
  137. function getTopWindow() {
  138. var p = window;
  139. while (p != p.parent) {
  140. p = p.parent;
  141. }
  142. return p;
  143. }
  144. function ajaxErrorInfo(jqXHR, textStatus, errorThrown) {
  145. if(textStatus == 'timeout'){
  146. alert('网络连接超时,请刷新您的网页。');
  147. }else {
  148. alert('url: ' + url +' error ' + textStatus + " " + errorThrown);
  149. }
  150. }