码迷,mamicode.com
首页 > Web开发 > 详细

$.ajax 常用的套路

时间:2018-07-31 21:50:48      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:oca   window   div   enc   llb   fun   obj   ++   return   

$.ajax 常用的套路

(function(){
    window.webApi = new Object();
    webApi.get = function(url,data,callback){
        $.ajax({
            type:‘GET‘,
            data: data,
            url: url,
            success: function(result){
                callback && callback(result)
            }
        })
    };

    webApi.formPost = function(url,data,callback){
        $.ajax({
            type:‘POST‘,
            data: data,
            contentType:‘application/x-www-form-urlencoded‘,
            url: url,
            success: function(result){
                callback && callback(result)
            }
        })
    };

    webApi.jsonPost = function(url,data,callback){
        $.ajax({
            type:‘POST‘,
            data: JSON.stringify(data),
            contentType:‘application/json‘,
            url: url,
            success: function(result){
                callback && callback(result)
            }
        })
    };
    window.getRequest = function(){
        var httpUrl = location.href; //获取url中‘?‘符后的字串
        var url = ‘?‘ + httpUrl.split(‘?‘)[1];
        var theRequest = {};
        if (url.indexOf(‘?‘) != -1) {
            var str = url.substr(1);
            var strs = str.split(‘&‘);
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split(‘=‘)[0]] = decodeURIComponent(strs[i].split(‘=‘)[1]);
            }
        }
        return theRequest;
    }
})()

  

$.ajax 常用的套路

标签:oca   window   div   enc   llb   fun   obj   ++   return   

原文地址:https://www.cnblogs.com/mingzhanghui/p/9397640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!