美文网首页
ajax封装

ajax封装

作者: 每天进步一点点5454 | 来源:发表于2020-10-10 14:54 被阅读0次

第一种

1 接口规范

function xiangzhenStationRank(item){
    var result={'flag':false,data:undefined};
    $.ajax({
        'async': false,
        'url': apiUrl + 'xxx',
        'dataType': 'json',
        'type': 'post',
        'data': {
            "access_token":xxx
          },
        success: function (res) {
            if (res.code == "0") {
                result.flag=true;
                result.data=res.data;
            }
        }
    });
    return result;
}

2 获取接口数据 并传参数

参数
let item = {}
item.name = 'aaa'
调用函数
var res = xiangzhenStationRank(item);
获取数据并判断
  if (res.flag) {
  var MaxRank = res.data
 需要的函数传数据`(MaxRank )`
  true 代码
}

第二种

        function zhangqiao(item) {
            $.ajax({
                async: false,
                url: Url + item.url,
                dataType: 'json',
                type: 'GET',
                data: item.param,
                success: function (res) {
                    var result = res;                      
                }
            });
            return result;
        }

aqi颜色判断

        let aqitext = '';
        if (aqiLevel == 1) {
            aqitext = '优';
            $('.wuranwu h6').css({ 'background': '#00E428' });
        } else if (aqiLevel == 2) {
            aqitext = '良';
            $('.wuranwu h6').css({ 'background': '#FFFF00' });
        } else if (aqiLevel == 3) {
            aqitext = '轻度污染';
            $('.wuranwu h6').css({ 'background': '#FF7E00' });
        } else if (aqiLevel == 4) {
            aqitext = '中度污染';
            $('.wuranwu h6').css({ 'background': '#FF0000' });
        } else if (aqiLevel == 5) {
            aqitext = '重度污染'
            $('.wuranwu h6').css({ 'background': '#830097' });
        } else if (aqiLevel == 6) {
            aqitext = '严重污染';
            $('.wuranwu h6').css({ 'background': '#7E0023' });
        }

相关文章

网友评论

      本文标题:ajax封装

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