123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /**
- * Created by Mai on 2017/4/20.
- */
- var CommonAjax = {
- get:function (url,data,cb,dataType) {
- $.get(url,data,cb,dataType)
- },
- post: function (url, data, successCallback, errorCallback) {
- $.ajax({
- type:"POST",
- url: url,
- data: {'data': JSON.stringify(data)},
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function(result){
- if (result.error === 0) {
- if (successCallback) {
- successCallback(result.data);
- }
- } else {
- alert('error: ' + result.message);
- if (errorCallback) {
- errorCallback();
- }
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- ajaxErrorInfo(jqXHR, textStatus, errorThrown);
- if (errorCallback) {
- errorCallback();
- }
- }
- });
- },
- compressPost: function (url, data, successCallback, errorCallback) {
- $.ajax({
- type:"POST",
- url: url,
- data: {'data': LZString.compress(JSON.stringify(data))},
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function(result){
- if (result.error === 0) {
- if (successCallback) {
- successCallback(result.data);
- }
- } else {
- alert('error: ' + result.message);
- if (errorCallback) {
- errorCallback();
- }
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- ajaxErrorInfo(jqXHR, textStatus, errorThrown);
- if (errorCallback) {
- errorCallback();
- }
- }
- });
- },
- postRationLib: function (url, data, successCallback, errorCallback) {
- $.ajax({
- type:"POST",
- url: url,
- data: data,
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function(result){
- if (!result.error) {
- if (successCallback) {
- successCallback(result.data);
- }
- } else {
- alert('error: ' + result.message);
- if (errorCallback) {
- errorCallback();
- }
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- ajaxErrorInfo(jqXHR, textStatus, errorThrown);
- if (errorCallback) {
- errorCallback();
- }
- }
- });
- },
- postEx: function(url, params, dftTimeOutMilSec, isAsync, successCallback, failCallback, exceptionCallback){
- $.ajax({
- type:"POST",
- url: url,
- data: {'params': JSON.stringify(params)},
- dataType: 'json',
- cache: false,
- async: isAsync,
- timeout: dftTimeOutMilSec,
- success: function(result){
- if (!result.error) {
- if (successCallback) {
- successCallback(result.data);
- }
- } else {
- alert('error: ' + result.message);
- if (failCallback) {
- failCallback();
- }
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- ajaxErrorInfo(jqXHR, textStatus, errorThrown);
- if (exceptionCallback) {
- exceptionCallback();
- }
- }
- });
- },
- specialPost:function (url, data,successCallback, errorCallback) {
- $.ajax({
- url: url,
- type: 'post',
- data: data,
- dataType: 'json',
- error: function(jqXHR, textStatus, errorThrown) {
- ajaxErrorInfo(jqXHR, textStatus, errorThrown);
- if (errorCallback) {
- errorCallback();
- }
- },
- success: function(response) {
- // 修改失败则恢复原值
- if (response.err !== 0) {
- if (errorCallback) {
- errorCallback(response);
- }
- } else {
- if (successCallback) {
- successCallback(response);
- }
- }
- }
- });
- }
- };
- /**
- * 设置全局的AJAX请求默认选项
- * 主要设置了AJAX请求遇到Session过期的情况
- */
- $.ajaxSetup({
- complete: function (data) {
- if (data.responseJSON&&data.responseJSON.ret_code && data.responseJSON.ret_code == 99) {
- alert(data.responseJSON.ret_msg);
- var top = getTopWindow();
- setTimeout('top.location.href = "/login";', 300);
- }
- }
- });
- async function ajaxPost(url, data, isPlainData = false) {
- return new Promise(function (resolve, reject) {
- $.ajax({
- type:"POST",
- url: url,
- data: isPlainData ? data : {'data': JSON.stringify(data)},
- dataType: 'json',
- cache: false,
- timeout: 200000,
- success: function(result){
- if (!result.error || commonUtil.isDef(result.err) && !result.err) {
- resolve(result.data);
- } else {
- const message = result.message || result.msg;
- alert('error: ' + message);
- reject(message);
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- ajaxErrorInfo(jqXHR, textStatus, errorThrown);
- reject("请求错误");
- }
- });
- });
- }
- async function ajaxGet(url,data){
- return new Promise(function (resolve, reject) {
- $.get(url,data,function (result,status) {
- if(status == 'success'){
- resolve(result);
- }else {
- reject(result);
- }
- },"json")
- })
- }
- /**
- * 在页面中任何嵌套层次的窗口中获取顶层窗口
- * @return 当前页面的顶层窗口对象
- */
- function getTopWindow() {
- var p = window;
- while (p != p.parent) {
- p = p.parent;
- }
- return p;
- }
- function ajaxErrorInfo(jqXHR, textStatus, errorThrown) {
- if(textStatus == 'timeout'){
- alert('网络连接超时,请刷新您的网页。');
- }else {
- alert('url: ' + url +' error ' + textStatus + " " + errorThrown);
- }
- }
|