封裝請求


//微信小程序封裝
// 封裝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;
					}
				})

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM