封装jQuery的ajax

来源:互联网 发布:淘宝网夏季竹凉席坐垫 编辑:程序博客网 时间:2024/05/18 03:58

学习代码,摘抄

function galasysJsonGet(url, json, callback, isAsync) {    var isAsync = isAsync == undefined ? false : isAsync;    if (url == '' || url == null || url == undefined) {        return false;    }    else {        if (typeof (json) == 'object') {            json = JSON.stringify(json);        }        var ajaxRequest = $.ajax({            url: url,            method: 'get',            datatype: 'json',            data: json,            contentType: 'application/json;charset=utf-8',            async: isAsync,            cache: false        });        ajaxRequest.done(function (msg) {            if (callback != null) {                callback(msg);            }        });        ajaxRequest.fail(function (jqXHR, textStatus) {            alert("Request failed: " + textStatus);        });    }}function galasysJsonPut(url, json, callback, isAsync) {    //默认异步    var isAsync = isAsync == undefined ? true : isAsync;    if (url == '' || url == null || url == undefined) {        return false;    }    else {        if (typeof (json) == 'object') {            json = JSON.stringify(json);        }        //ajax请求        var ajaxRequest = $.ajax({            url: url,            method: 'put',            datatype: 'json',            data: json,            contentType: 'application/json;charset=utf-8',            async: isAsync,            cache: false        });        ajaxRequest.done(function (msg) {            if (callback != null) {                callback(msg);            }        });        ajaxRequest.fail(function (jqXHR, textStatus) {            alert("Request failed: " + textStatus);        });    }}function galasysJsonPost(url, json, callback, isAsync) {    //默认异步    var isAsync = isAsync == undefined ? false : isAsync;    if (url == '' || url == null || url == undefined) {        return false;    }    else {        if (typeof (json) == 'object') {            json = JSON.stringify(json);        }        //ajax请求        var ajaxRequest = $.ajax({            url: url,            method: 'post',            datatype: 'json',            data: json,            contentType: 'application/json;charset=utf-8',            async: isAsync,            cache: false        });        ajaxRequest.done(function (msg) {            if (callback != null) {                callback(msg);            }        });        ajaxRequest.fail(function (jqXHR, textStatus) {            alert("Request failed: " + textStatus);        });    }}
原创粉丝点击