Vue axios 请求方式汇总

1.发送form-data形式

let formData = new FormData();
formData.append('username', data.username); // 添加键值对
formData.append('password', data.password); // 添加键值对
axios.post('http:/xxx/cms-admin/user/login', formData).then(response => {
    console.log("data:", response.data)
    if (response.data.status != 0) {
        ElMessage.error(response.data.errmsg)
        return
    }

}).catch(error => {
    console.log("error:", error)
})

2.发送json

post请求 [axios post的默认请求头是 application/json]

const data = reactive({
    username: '',
    password: '',
})

axios.post('http:/xxx/cms-admin/user/login', data).then(response => {
    console.log("data:", response.data)
    if (response.data.status != 0) {
        ElMessage.error(response.data.errmsg)
        return
    }

}).catch(error => {
    console.log("error:", error)
})

 

posted on 2024-07-29 21:03  星河赵  阅读(21)  评论(0编辑  收藏  举报

导航