uniapp post请求 formdata
Uniapp的post请求可以使用axios库来发送请求,也可以使用uni.request方法发送请求。如果要发送formdata数据,可以使用FormData对象来构造请求体,并设置请求头的Content-Type为multipart/form-data。
示例代码:
// 使用axios发送post请求
import axios from 'axios'
let formData = new FormData()
formData.append('name', '张三')
formData.append('age', 20)
axios({
method: 'post',
url: 'http://example.com/api/user',
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(res => {
console.log(res.data)
}).catch(err => {
console.error(err)
})
// 使用uni.request发送post请求
let formData = new FormData()
formData.append('name', '张三')
formData.append('age', 20)
uni.request({
method: 'POST',
url: 'http://example.com/api/user',
data: formData,
header: {
'Content-Type': 'multipart/form-data'
},
success: (res) => {
console.log(res.data)
},
fail: (err) => {
console.error(err)
}
})
原文地址: https://www.cveoy.top/t/topic/0mm 著作权归作者所有。请勿转载和采集!