美文网首页
jQuery扩展ajax操作

jQuery扩展ajax操作

作者: 情话_2ee5 | 来源:发表于2018-08-07 15:29 被阅读0次

/**

* jq操作扩展

* @param $

*/

(function($){

/**

* 判断字符串是否为空

*/

$.isEmptyStr =function(str){

if(null!= str &&''!= str &&'undefined'!= str)

returnfalse;

returntrue;

    };

/**

* 获取url中的参数

*/

$.getUrlParam =function(url, name){

vartheRequest = {};

if(url.indexOf("?") !=-1) {

varstr = url.substr(url.indexOf("?") +1);

strs = str.split("&");

for(vari =0; i < strs.length; i ++) {

varkey = strs[i].substr(0, strs[i].indexOf('='));

varval = strs[i].substr(strs[i].indexOf('=') +1, strs[i].length);

theRequest[key]= val;

}

}

returntheRequest[name];

    };

/**

* 判断数组是否为空

*/

$.isEmptyArray =function(list){

if(list && listinstanceofArray&& list.length >0)

returnfalse;

returntrue;

    };

/**

    * 对ajax进行简单封装

    * url 发送请求的地址

    * params 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}

    * async 默认值: true。默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。

    * 注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。

    * dataType 默认 application/json;charset=utf-8,常用的如:application/json;charset=utf-8(json)、application/x-www-form-urlencoded; charset=UTF-8(form)

    * contentType 默认json, 预期服务器返回的数据类型,常用的如:xml、html、json、text

    * successfn 成功回调函数

    * errorfn 失败回调函数

    */

$.ajaxReq =function(url, params, successfn, conentType, dataType, async, errorfn){

dataType = (dataType ==null|| dataType ==""||typeof(dataType) =="undefined") ?"json": dataType;

params = (params ==null|| params ==""||typeof(params) =="undefined") ? {} : params;

async= (async==null||async==""||typeof(async) =="undefined") ?"true":async;

contentType = (conentType ==null|| conentType ==""||typeof(conentType) =="undefined") ?"application/json;charset=utf-8":"application/x-www-form-urlencoded";

        $.ajax({

type:"post",

async:async,

data: params,

url: url,

timeout:60000,

contentType: contentType,

dataType: dataType,

success:function(d){

if($.isFunction(successfn))

            successfn(d);

            },

error:function(e){

if($.isFunction(errorfn))

            errorfn(e);

            }

        });

    };

/**

    * url 发送请求的地址

    * params 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}

    * return 返回json数据

    */

$.getData =function(url, params){

varretData = {};

params = (params ==null|| params ==""||typeof(params) =="undefined") ? {} : params;

        $.ajax({

type:"post",

url:url,

dataType:"json",

data:params,

cache:false,

async:false,

timeout:60000,

success:function(data){

            retData = data;

            },

error:function(err){

                retData.exception = err;

retData.msg = err.responseText ||"请求失败";

            }

        });

returnretData;

    }

/**

    * url 发送请求的地址

    * params 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}

    * return 返回html界面

    */

$.loadHtml =function(url, params){

varhtml ="";

params = (params ==null|| params ==""||typeof(params) =="undefined") ? {} : params;

        $.ajax({

type:"get",

url: url,

dataType:"html",

data: params,

cache:false,

async:false,

timeout:60000,

success:function(data){

            html = data;

            },

error:function(err){

html = err.responseText ||"请求失败";

            }

        });

returnhtml;

    }

})(jQuery);

相关文章

  • jQuery扩展ajax操作

    /** * jq操作扩展 * @param $ */ (function($){ /** * 判断字符串是否为空 ...

  • 事件冒泡 委托 节点 ajax jsonp

    事件冒泡 事件委托 节点操作 ajax ajax jQuery-jsonp

  • 2018-12-10

    节点操作 ajax jsonp jQuery-jsonp jsonp公开接口

  • day9-jQuery

    十七.什么是jQuery 十八.jQuery属性操作和样式 十九.jQuery的动态添加和删除 20.Ajax

  • jQuery——Ajax

    jQuery对ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第二层是load()、...

  • 二、Jquery 选择器

    选择器是 Jquery 的基础,在 Jquery 中,对事件的处理、DOM 操作、Ajax 操作都是依赖选择器。 ...

  • jquery ajax操作

    1, jQuery load() 方法是简单但强大的 AJAX 方法。 load()方法默认使用 GET 方式, ...

  • Jquery Ajax 封装

    Jquery Ajax封装 Jquery Ajax 调用

  • Ajax实现登陆验证

    关于jquery与Ajax jQuery 提供多个与 AJAX 有关的方法。通过 jQuery AJAX 方法,能...

  • ajax使用注意

    新版jQuery使用ajax: 旧版jQuery使用ajax:

网友评论

      本文标题:jQuery扩展ajax操作

      本文链接:https://www.haomeiwen.com/subject/wzhavftx.html