I encountered a problem today. The front-end post request parameters have obviously been passed, but the back-end said that it has not been received. I don’t know if the back-end brother deliberately messed with me .
The code + front-end picture is as follows↓
the code
import axios from 'axios'
//对象形式
const val = {
pass:'123',user:'name'}
axios.post('/api/login', val).then(res=>console.log(res))
picture
I clearly passed it on, but the backend said it was my problem.
Solution
- Use the qs module to convert the parameter object into
?user=xxx&pas=xxxx
a form, and then splice it with the request link, for example:/login?user=xxxx&pas=xxx
- Note: QS module axios comes with it, no need to download it again
demo
import axios from 'axios'
import Qs from 'qs'//导入qs
//对象形式
const val = {
pass:'123',user:'name'}
//重点:将对象转为user=xxx&pas=xxxx形式
const query = Qs.stringify(val)
//用?号拼接起来再传给后端
axios.post('/api/login?' + query)
You can also traverse the parameter objects and stitch them yourself, but because you are too lazy, you use the qs module!