zoukankan      html  css  js  c++  java
  • 在wepy框架中 使用promise对发送网络请求进行封装 包括post跟get请求

    // ====utils.js

      //==封装post请求

    const post = (url,data) =>{

      let promise = new Promise((resolve,reject)=>{

        wepy.request({

          url: url,

          data: data,

          header:{'content-type':'applicction/x-www-form-urlencoded'}  或者是 header{'content-type':'application/json'},

          success: res=>{

            if(res.statusCode ==200){

              resolve(res)

            }else {

              reiect(res)

            }

          },

          fail: res=>{

            reject(res)

          }

        })

      })

      return promise

    }

    //====封装get请求

    const get =(url,data)=>{

      let promise = new Promise((resolve,reject)=>{

        wepy.request({

          url: url,

          data: data,

          header: {'content-type': 'application/x-www-form-urlencoded'}  或者是  header: {'content-type': 'application/json'},

          success: res=>{

            if(res.statusCode ==200){

              resolve(res)

            }else {

              reject(res)

            }

          },

          fail: res=>{

            reject(res)

          }

        })

      })

      return promise

    }

    module.exports = {

      post: post,

      get: get

    }

    //=======引用封装的请求

    const utils = require('../utils.js')

    utils.post(url,data).then(res=>{

      console.log(res)   //====请求成功后

    }).catch(res=>{

      console.log(res)  //====失败后的返回

    })

    /// get请求是同理的

  • 相关阅读:
    MySQLFront导入SQL文件报#1113错误解决
    LNMP1.3一键安装Linux环境,配置Nginx运行ThinkPHP3.2
    币胜网虚拟货币交易平台安装说明
    windows服务器详细安全设置
    WINDOWS SERVER 2008远程桌面端口修改方法
    mac终端ssh连接服务器 空闲的时候 连接断开
    FTP软件发送"AUTH TLS"提示 无法连接到服务器
    LNMP状态管理命令
    lnmp1.4环境FTP服务器的安装和使用
    springCloud
  • 原文地址:https://www.cnblogs.com/PinkYun/p/9849330.html
Copyright © 2011-2022 走看看