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
dov.defaults.baseURL = 'http://www.baidu.com'
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
})