WeChat applet uses Dov.js to implement asynchronous requests

Use Dov.js to implement asynchronous requests


  • Dov.js address: https://gitee.com/DocBug/dov-http-mini

1. Introduce Dov.js

  • Download: npm install dov-http-mini
  • Copy the dov.min.js file into your own project
  • Import in the required page js file

2. Quick use

//  设置默认地址(和axios的使用方法一致)
dov.defaults.baseURL = 'http://www.baidu.com'   
//  get请求(请求地址为http://www.baidu.com/user)
dov.get('user').then(response => {
    
    
    console.log(response)
})

//  发送请求时可以添加参数
dov.get('http://www.baidu.com/user', {
    
    
//  参数
    data: {
    
    
        username: 'dov',
        password: 'asdkln211232345sa'
    }
}).then(response => {
    
    
    console.log(response)
})

dov api


dov({
    
    
  method: 'post',
  url: 'http://www.baidu.com/getUserInfo',
  data: {
    
    
    username: 'king',
    password: 'kingpassword'
  }
}).then(response => {
    
    
  console.log(response)
})

3. Request method in WeChat Mini Program

  • GET
  • POST
  • PUT
  • DELETE,
  • OPTIONS,
  • HEAD,
  • TRACE,
  • CONNECT

4. Create instance objects

let server1 = dov.create({
    
    
    baseURL: 'https://api.baidu.com'
})
let server2 = dov.create({
    
    
    baseURL: 'https://img.baidu.com'
})

server1.get('/getUserInfo').then(response => {
    
    
    console.log(response)
})

Configure interceptor

dov.interceptors.request.use(function (config) {
    
    
    config.data.token = wx.getStorageSync('token')
    // ...
    return config
})

Guess you like

Origin blog.csdn.net/weixin_40944062/article/details/105030305