| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /*
- * 描 述:ajax操作方法
- */
- (function ($, learun) {
- "use strict";
- var httpCode = {
- success: 1,
- fail: 0,
- exception: -1,
- nologin: 410 // 没有登录者信息
- };
- var exres = { code: -1, info: '通信异常,请联系管理员!' }
- function isLogin(res) {
-
- if (res.code == learun.httpCode.nologin) {
- var _topUrl = top.$.rootUrl + '/Login/Index';
- switch (res.info) {
- case 'nologin':
- break;
- case 'noip':
- _topUrl += '?error=ip';
- break;
- case 'notime':
- _topUrl += '?error=time';
- break;
- case 'other':
- _topUrl += '?error=other';
- break;
- }
- top.window.location.href = _topUrl;
- return false;
- }
- return true;
- }
- function httpHeaders() {
- var headers = {
- token: $.lcoreUser.token,
- /* "Content-Type": "application/json"*/
- }
- return headers;
- }
- $.extend(learun, {
- // http 通信异常的时候调用此方法
- httpErrorLog: function (msg) {
- learun.log(msg);
- },
- // http请求返回数据码
- httpCode: httpCode,
- // get请求方法(异步):url地址,callback回调函数
- httpAsyncGet: function (url, callback) {
- $.ajax({
- url: url,
- headers: httpHeaders(),
- type: "GET",
- dataType: "json",
- async: true,
- cache: false,
- success: function (res) {
- isLogin(res);
- callback(res);
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- callback(exres);
- },
- beforeSend: function () {
- },
- complete: function () {
- }
- });
- },
- // get请求方法(同步):url地址,param参数
- httpGet: function (url, param) {
- var res = {};
- $.ajax({
- url: url,
- headers: httpHeaders(),
- data: param,
- type: "GET",
- dataType: "json",
- async: false,
- cache: false,
- success: function (res) {
- isLogin(res);
- return res;
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- return exres;
- },
- beforeSend: function () {
- },
- complete: function () {
- }
- });
- return res;
- },
- // post请求方法(异步):url地址,param参数,callback回调函数
- httpAsyncPost: function (url, param, callback) {
- $.ajax({
- url: url,
- headers: httpHeaders(),
- data: param,
- type: "POST",
- dataType: "json",
- async: true,
- cache: false,
- success: function (res) {
- isLogin(res);
- callback(res);
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- callback(exres);
- },
- beforeSend: function () {
- },
- complete: function () {
- }
- });
- },
- // post请求方法(同步步):url地址,param参数,callback回调函数
- httpPost: function (url, param, callback) {
- $.ajax({
- url: url,
- headers: httpHeaders(),
- data: param,
- type: "POST",
- dataType: "json",
- async: false,
- cache: false,
- success: function (res) {
- isLogin(res);
- callback(res);
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- callback(exres);
- },
- beforeSend: function () {
- },
- complete: function () {
- }
- });
- },
- // ajax 异步封装
- httpAsync: function (type, url, param, callback) {
- $.ajax({
- url: url,
- headers: httpHeaders(),
- data: param,
- type: type,
- dataType: "json",
- async: true,
- cache: false,
- success: function (res) {
- if (!isLogin(res)) {
- callback(null);
- } else {
- callback(res);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- callback(exres);
- },
- beforeSend: function () {
- },
- complete: function () {
- }
- });
- },
- // ajax 同步封装
- httpSync: function (type, url, param, callback) {
- $.ajax({
- url: url,
- headers: httpHeaders(),
- data: param,
- type: type,
- dataType: "json",
- async: false,
- cache: false,
- success: function (res) {
- if (!isLogin(res)) {
- callback(null);
- }
- else {
- callback(res);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
-
- callback(exres);
- },
- beforeSend: function () {
- },
- complete: function () {
- }
- });
- },
- deleteForm: function (url, param, callback) {
- learun.loading(true, '正在删除数据');
- learun.httpAsyncPost(url, param, function (res) {
- learun.loading(false);
- if (res.code > 0) {
- if (!!callback) {
- callback(res);
- }
- learun.alert.success(res.info);
- }
- else {
- learun.alert.error(res.info);
- }
- layer.close(layer.index);
- });
- },
- postForm: function (url, param, callback) {
- learun.loading(true, '正在提交数据');
- learun.httpAsyncPost(url, param, function (res) {
- learun.loading(false);
- if (res.code > 0) {
- if (!!callback) {
- callback(res);
- }
- learun.alert.success(res.info);
- }
- else {
- learun.alert.error(res.info);
- }
- layer.close(layer.index);
- });
- }
- });
- })(window.jQuery, top.learun);
|