Axios请求方法及别名(post方法,put方法和patch方法)

post,put,patch都比较相似,都可以form-data,applicition/json请求

<template>
  <div class="axios"></div>
</template>
<script>
import axios from 'axios'

export default {
  name: 'axios2-2',
  created() {
    /*
      post方法:提交数据(表单提交+文件上传)
      form-data 表单提交(图片上传,文件上传)
      applicition/json
    */
    let data = {
      id: 12
    }
    axios.post('/post', data).then(res => {
      console.log(res)
    })

    axios({
      method: 'post',
      url: '/post', // 虚拟路径
      data: data
    }).then(res => {
      console.log(res)
    })
    
    // form-data请求
    let formData = new FormData()
    for(let key in data){
      formData.append(key, data[key])
    }
    axios.post('/post', formData).then(res => {
      console.log(res)
    })
  },
}
</script>

1、applicition/json请求

    // applicition/json请求
    let data = {
      id: 12
    }
    axios({
      method: 'post',
      url: '/post', // 虚拟路径
      data: data
    }).then(res => {
      console.log(res)
    })

在这里插入图片描述
2、form-data请求

    let data = {
      id: 12
    }
    // form-data请求
    let formData = new FormData()
    for(let key in data){
      formData.append(key, data[key])
    }
    axios.post('/post', formData).then(res => {
      console.log(res)
    })

在这里插入图片描述
put请求

    // put请求
    let data = {
      id: 12
    }
    axios.put('/put', data).then(res => {
      console.log(res)
    })

patch请求

    // patch请求
    let data = {
      id: 12
    }
    axios.patch('/patch', data).then(res => {
      console.log(res)
    })
发布了36 篇原创文章 · 获赞 12 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_35697034/article/details/100555225