• 微信小程序之上传下载交互api


    wx.request(OBJECT)

    OBJECT参数说明:

    参数名类型必填说明
    url String 开发者服务器接口地址
    data Object、String 请求的参数
    header Object 设置请求的 header , header 中不能设置 Referer
    method String 默认为 GET,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    dataType String 默认为 json。如果设置了 dataType 为 json,则会尝试对响应的数据做一次 JSON.parse
    success Function 收到开发者服务成功返回的回调函数,res = {data: '开发者服务器返回的内容'}
    fail Function 接口调用失败的回调函数
    complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

    success返回参数说明:

    参数说明最低版本
    data 开发者服务器返回的数据  
    statusCode 开发者服务器返回的状态码  
    header 开发者服务器返回的 HTTP Response Header 1.2.0

    data 数据说明 最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:

    • 对于 header['content-type'] 为 'application/json' 的数据,会对数据进行 JSON 序列化
    • 对于 header['content-type'] 为 'application/x-www-form-urlencoded' 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)

    示例代码:

    wx.request({  url: 'test.php', //仅为示例,并非真实的接口地址  data: {     x: '' ,     y: ''  },  header: {

          'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
      }
    })
    微信小程序开发时,使用公共的url端口,自定义函数

    module.exports = {

    // API 接口
    API_HOST: "url/"

    }

    const config = require('../config.js');

    module.exports = {

    //get方式请求

    GET: function (url = '', data = {}, fn) {

      console.log(data);

      wx.request({

      url: config.API_HOST + url,//请求地址

      method: 'get',//请求方式

      data: data,//请求参数

      header: { "Content-Type": "application/x-www-form-urlencoded" },

      success: function (res) {

      fn(res);

    }

    });

    },

    //post方式请求
    POST: function (url = '', data = {}, fn) {
    wx.request({
      url: config.API_HOST + url,//请求地址
      method: 'post',//请求方式
      data: data,//请求参数
      header: { "Content-Type":"application/x-www-form-urlencoded"},
      success: function (res) {
      fn(res);
      }
    });
    },

    }
     
     1 // 加载配置文件
     2 const config = require('config.js');
     3 var app=getApp();
     4 module.exports = {
     5   /**
     6    * get方式请求,ulr是请求api号,token是登陆token,不用token就传空,fn是函数成功的回调函数,data为向后台传递的参数by:张涛20180303
     7    */
     8   GET: function (url = '',token='' ,data = {}, fn,fail) {
     9     wx.request({
    10       url: config.API_HOST + url,//请求地址
    11       method: 'get',//请求方式
    12       data: data,//请求参数
    13       header: { "Content-Type": "application/json" ,'token':token},
    14       success: function (res) {
    15         // 判断token是否失效
    16         if (res.data.code=='JWT00002'||res.data.code=='JWT00001'||res.data.code=='JWT00004'||res.data.code=='403') {
    17           wx.navigateTo({
    18               url:'/pages/login/login'
    19           })
    20           return false;
    21         }
    22         fn(res);
    23       },
    24       fail: function (res) {
    25         // wx.showToast({
    26         //   title: '请求服务器失败,请稍后再试!',
    27         //   icon: 'loading',
    28         //   duration: 2000
    29         // })
    30       }
    31     });
    32   },
    33 
    34   /**
    35    * post方式请求
    36    */
    37   POST: function (url = '',token='', data = {}, fn ,fail) {
    38     wx.request({
    39       url: config.API_HOST + url,//请求地址
    40       method: 'post',//请求方式
    41       data: data,//请求参数
    42       header: { "Content-Type": "application/json",'token':token},
    43       success: function (res) {
    44         // 判断token是否失效 如果失效就跳转登录页面
    45         if (res.data.code=='JWT00002'||res.data.code=='JWT00001'||res.data.code=='JWT00004'||res.data.code=='403') {
    46           wx.navigateTo({
    47               url:'/pages/login/login'
    48           })
    49           return false;
    50         }
    51         fn(res);
    52       },
    53       fail: function (res) {
    54         // wx.showToast({
    55         //   title: '请求服务器失败,请稍后再试!',
    56         //   icon: 'loading',
    57         //   duration: 2000
    58         // })
    59       }
    60     });
    61   }
    62 
    63 }

    增加个人封装的小程序请求方法

  • 相关阅读:
    [基础]编程环境配置
    MonoDevelop line endings
    Unity手机平台播放影片
    [3D跑酷] GUIManager UI管理
    [3D跑酷] UI事件处理系统
    [3D跑酷] AudioManager
    NGUI学习笔记汇总
    Unity3D开发之搭建Mac OS开发环境
    Unity键值(KeyCode)
    Unity3D多人协作开发环境搭建
  • 原文地址:https://www.cnblogs.com/bluesky1024/p/7127820.html
Copyright © 2020-2023  润新知