• 封装请求


    //微信小程序封装
    // 封装psot请求
    const post = (url, data) => {
      var promise = new Promise((resolve, reject) => {
        //网络请求
        wx.request({
          url: 'https://tp.xiniuwangluo.cn/' + url,
          data: data,
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
          },
          success: function (res) {//服务器返回数据
            if (res.statusCode == 200) {
              resolve(res);
            } else {//返回错误提示信息
              reject(res.data);
            }
          },
          error: function (e) {
            reject('网络出错');
          }
        })
      });
      return promise;
    }
    // get请求封装
    function getRequest(url, data) {
      var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        wx.request({
          url: 'https://tp.xiniuwangluo.cn/' + url,
          data: postData,
          method: "GET",
          dataType: "json",
          header: {
            // "content-type": "application/json"
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
    
          },
          success: function (res) {
            if (res.statusCode == 200) {
              resolve(res.data);
            } else {
              resolve(res.data);
            }
          },
          error: function (e) {
            reject("网络出错");
          }
        });
      });
      return promise;
    }
    //抛出
    module.exports = {
      post,
      get: getRequest,
    }
    //js 引入文件
    let utils = require('utils/http.js');
    //调用
    // 请求分类
      utils.post(url, {}).then(res => {
        // console.log(res);
      })
    utils.post(url, {}).then(res => {
               console.log(res.data);
        })
    // uniapp
    const commonUrl = "域名"; //公共路径 
    
    // post请求封装
    function postRequest(url, data) {
        var promise = new Promise((resolve, reject) => {
            var that = this;
            var postData = data;
            uni.request({
                url: commonUrl + url,
                data: postData,
                method: "POST",
                header: {
                    // 'Content-Type': 'application/x-www-form-urlencoded'
    				'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
                    // token: uni.getStorageSync("token")
                },
                success: function(res) {
                    //返回什么就相应的做调整
                    if (res.statusCode == 200) {
                        resolve(res.data);
                    } else {
                        // 请求服务器成功,但是由于服务器没有数据返回,此时无code。会导致这个空数据
                        //接口后面的then执行
                        // 不下去,导致报错,所以还是要resolve下,这样起码有个返回值,
                        //不会被阻断在那里执行不下去!
                        resolve(res.data.msg);
                    }
                },
                error: function(e) {
                    reject("网络出错");
                }
            });
        });
        return promise;
    }
    
    // get请求封装
    function getRequest(url, data) {
        var promise = new Promise((resolve, reject) => {
            var that = this;
            var postData = data;
            uni.request({
                url: commonUrl + url,
                data: postData,
                method: "GET",
                dataType: "json",
                header: {
                    "content-type": "application/json"
                },
                success: function(res) {
                    if (res.statusCode == 200) {
                        resolve(res.data);
                    } else {
                        resolve(res.data);
                    }
                },
                error: function(e) {
                    reject("网络出错");
                }
            });
        });
        return promise;
    }
    module.exports = {
        post: postRequest,
        get: getRequest,
    }
    //调用
    request.get/post('url', {}).then(res => {
    					// console.log(res);
    					if (res.return_code == '1000') {
    						that.hyfl = res.fllist;
    					}
    				})
    

      

  • 相关阅读:
    fopen flock fclose 文件用法
    thinkphp并发 阻塞模式与非阻塞模式
    thinkphp3.2 控制器导入模型
    thinkphp3.2 session时间周期无效
    UWP滑动后退
    旺信UWP公测邀请
    旺信UWP倒计时
    UWP应用开发系列视频教程简介
    新浪微博UWP UI意见征求
    淘宝UWP--自定义图片缓存
  • 原文地址:https://www.cnblogs.com/8023-CHD/p/13712414.html
Copyright © 2020-2023  润新知