国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

封裝了jQuery的Ajax請(qǐng)求全局配置

2019-11-20 13:13:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

摘要:

  jQuery已經(jīng)成為項(xiàng)目中最常見的js庫(kù),也是前端開發(fā)最喜歡使用的庫(kù)。下面是在項(xiàng)目中封裝了jQuery的Ajax,分享給大家。

代碼:

復(fù)制代碼 代碼如下:

// ajax 請(qǐng)求參數(shù)
var ajaxSettings = function(opt) {
    var url = opt.url;
    var href = location.href;
    // 判斷是否跨域請(qǐng)求
    var requestType = 'jsonp';
    if (url.indexOf(location.host) > -1)
        requestType = 'json';
    requestType = opt.dataType || requestType;
    // 是否異步請(qǐng)求
    var async = (opt.async === undefined ? true : opt.async);
    return {
        url: url,
        async: async,
        type: opt.type || 'get',
        dataType: requestType,
        cache: false,
        data: opt.data,
        success: function(data, textStatus, xhr) {
            /*
            *如果dataType是json,怎判斷返回?cái)?shù)據(jù)是否為json格式,如果不是進(jìn)行轉(zhuǎn)換
            * 成功數(shù)據(jù)通用格式
            *   {
            *       "code": 200,
            *       "data": [],
            *       "success": true // 成功
            *   }
            *   失敗返回的數(shù)據(jù)
            *   {
            *       "code": 200,
            *       "info": 'error',
            *       "success": false // 失敗
            *   }
             */
            if((requestType === 'json' || requestType === "jsonp") && typeof(data) === "string") {
                data = JSON.parse(data);
            }
            if(data.success) {
                opt.success(data);
            }

            if(opt.error) {
                opt.error(data);
            }

        },
        error: function(xhr, status, handler) {
            if (opt.error)
                opt.error();
        }
    };
};
function unescapeEntity(str) {
    var reg = /&(?:nbsp|#160|lt|#60|gt|62|amp|#38|quot|#34|cent|#162|pound|#163|yen|#165|euro|#8364|sect|#167|copy|#169|reg|#174|trade|#8482|times|#215|divide|#247);/g,
        entity = {
        ' '   : ' ',
        ' '   : ' ',
        '<'     : '<',
        '<'    : '<',
        '>'     : '>',
        '&62;'     : '>',
        '&'    : '&',
        '&'    : '&',
        '"'   : '"',
        '"'    : '"',
        '¢'   : '¢',
        '¢'   : '¢',
        '£'  : '£',
        '£'   : '£',
        '¥'    : '¥',
        '¥'   : '¥',
        '€'   : '

主站蜘蛛池模板: 萝北县| 纳雍县| 楚雄市| 博湖县| 中宁县| 盘锦市| 丰顺县| 宁陕县| 揭阳市| 新源县| 仁化县| 青冈县| 五指山市| 郴州市| 柳林县| 巢湖市| 惠东县| 射洪县| 黑水县| 湄潭县| 中山市| 青阳县| 太仓市| 手游| 谷城县| 建平县| 定日县| 金溪县| 尼勒克县| 铅山县| 阳东县| 长顺县| 台州市| 通山县| 安岳县| 南昌市| 灵宝市| 集安市| 禹州市| 天等县| 凤凰县|