Posts Vue后端传参
Post
Cancel

Vue后端传参

Get 与 Post 请求的区别

今天无意中发现 Get 请求也是可以封装请求体的,百度了一下,两者的主要区别。

  1. GET 请求没有请求体(并不准确)。POST 请求有请求体。
  2. GET 请求的参数(需要传递的数据)要放在 URL 中发送,大小有限制。 POST 请求的参数可以放在 URL 后传递,也可以放在请求体中(大小不受限制)。
  3. Get 请求会被浏览器缓存。 且数据为明文。
  4. 使用方式: Get 请求用来查询,参数量小。 Post 请求用来插入更新,参数量大。

Get 的请求方式与参数的接收

  1. Get 请求带请求体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 请求 -->
    input: {
      username:'周杰伦',
      password:'123456'
    },

  const data  = await this.$http.get('img',{
    params: this.input
  })

  // 接收  用对象接收
  public String  img(User user){

  }
  1. Get 请求在 url 字符串后拼接
1
2
3
4
5
6
7
8
9
10
11
12
// 请求
方式1. const data  = await this.$http.get('img/'+this.page)
// 接收
@GetMapping("/img/{id}")
    public String getMethon(@PathVariable Integer id){

    }

方式2.  参数放url中间
const data  = await this.$http.get(`img/${this.input.password}/demo`)


Post 请求与接收

  1. 请求体封装为对象直接传输,后端以对象接收
1
2
3
4
5
6
7
// 请求
    const data  = await this.$http.post('/img',this.input)

    }
// 接收
   @PostMapping("/img")
    public String  img(@RequestBody User user){}
  1. 请求体没有做封装, 字段如果相对应即可。
1
2
3
4
5
6
7
8
9
// 请求
    const data  = await this.$http.post('/img',{
      username:this.input.username,
      password:this.input.password
    })

// 接收
   @PostMapping("/img")
    public String  img(@RequestBody User user){}

post和get传参数的方式是不一样的,写post的时候如果在params里面传参数,后台是收不到的

这篇博客写的还挺详细的

This post is licensed under CC BY 4.0 by the author.

Contents

Trending Tags