
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
基于 axios 封装的微信小程序请求工具,完全使用Promise,并提供了请求和响应的拦截器。
import axios from './libs/mini-axios.min.js'
// http.js
import axios from './libs/mini-axios.min.js'
axios.get(url).then(res => {
console.log(res )
})
// 或者
axios.defaults.baseURL = 'https://xxx.com/' // 配置默认地址
axios.get(url).then(res => {
console.log(res)
})
==默认已经绑定App,Page,Component 与导入的axios是同一个实例==
//只需要在app.js中导入就好
--app.js
import axios from './libs/mini-axios.min.js'
App({
onLaunch: function () {
this.$axios.get(url).then(res => {
console.log(res)
})
}
})
--pages/index/index.js
//不需要再导入了 直接使用
Page({
onLoad:function(){
this.$axios.post(url).then(res => {
console.log(res)
})
}
})
let data = {
userId: 1,
username:"张三"
};
axios.get(url, data).then(res=> {
console.log(res)
})
function getUserInfo(){
return axios.get(url)
}
function getList(){
return axios.get(url)
}
axios.all([getUserInfo, getList]).then(res=> {
console.log(res)
}).catch(error => {
console.log(error)
})
axios.race([getUserInfo, getList]).then(res=> {
console.log(res)
}).catch(error => {
console.log(error)
})
axios(config)
axios({
method: 'POST',
url: url,
data: {
userId:1
}
}).then(res=> {
console.log(res)
}).catch(err => {
console.log(err)
})
// 或
axios.defaults.baseURL = 'https://xxx.com'
axios({
method: 'POST',
url: url,
data: {
userId:1
}
}).then(res=> {
console.log(res)
})
axios.(url[,config])
axios(url, {
method: 'POST',
data: {
userId:1
}
}).then(res=> {
console.log(res)
})
小程序中定义了8种请求类型:微信小程序请求方式
let server1 = axios.create({
baseURL: 'https://xxx.com'
})
let server2 = axios.create({
baseURL: 'https://xxx.com'
})
server1.get(url).then(res => {
console.log(res)
})
部分配置参数和微信小程序的配置一样 微信小程序 request 请求参数列表
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
baseURL | 默认地址 baseURL 将自动加在 url 前面 | string | '' |
url | url 是用于请求的服务器 URL | string | '' |
data | data 是作为请求主体被发送的数据 | object | {} |
header/headers | headers 是即将被发送的自定义请求头 | object | 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' |
method | method 是创建请求时使用的方法 | string | 'GET' |
dataType | dataType 是返回的数据类型 | string | 'json' |
responseType | responseType 是响应的数据类型 | string | 'text' |
axios.defaults.baseURL = 'https://xxx.com'
mini-axios 也提供了和 axios 一样的请求拦截和响应拦截,并且可以配置多个
// 1.第一个 request 的拦截器
axios.interceptors.request.use(config => {
config.data.companyId = 1
// ...do something
return config
})
// 2.第二个 request 的拦截器,
axios.interceptors.request.use(config => {
config.data.token = wx.getStorageSync('token')
// ...do something
return config
})
// 1.第一个 response 的拦截器
axios.interceptors.response.use(response => {
if (response.statusCode === 200) {
// ...do something
}
console.log(response.header);
return response
})
// 2.第二个 response 的拦截器,
axios.interceptors.response.use(function (response) {
if (response.status === 400) {
// ...do something
}
return response
})
MIT
FAQs
基于 axios 封装的微信小程序请求工具,完全使用Promise,并提供了请求和响应的拦截器
We found that mini-axios demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.