关于前端以post 或 get 方式传参以及后端如何接收对应参数的几种形式

1:前端调用接口地址 this. h t t p . g e t ( ′ / a p i / u s e r / d e t a i l ′ , p a r a m s : i d ) 后 端 接 收 到 的 请 求 就 是 h t t p : / / l o c a l h o s t : 8080 / a p i / u s e r / d e t a i l ? i d = 1 @ G e t M a p p i n g ( v a l u e = " / d e t a i l " ) p u b l i c R e s p < O b j e c t > d e t a i l ( @ R e q u e s t P a r a m L o n g i d ) r e t u r n R e s p . s u c c e s s ( m y U s e r S e r v i c e . d e t a i l ( i d ) ) ; 2 : c o n s t i d = 1 前 端 调 用 接 口 地 址 t h i s . http.get('/api/user/detail', {params:{id}}) 后端接收到的请求就是http://localhost:8080/api/user/detail?id=1 @GetMapping(value = "/detail") public Resp<Object> detail(@RequestParam Long id) { return Resp.success(myUserService.detail(id)); } 2:const id = 1 前端调用接口地址 this. http.get(/api/user/detail,params:id)http://localhost:8080/api/user/detail?id=1@GetMapping(value="/detail")publicResp<Object>detail(@RequestParamLongid)returnResp.success(myUserService.detail(id));2constid=1this.http.get(’/api/user/detail/’ + id)
后端接收到的请求就是http://localhost:8080/api/user/detail/1
@GetMapping(value = “/detail1/{id}”)
public Resp detail1(@PathVariable(“id”) Long id) {
return Resp.success(myUserService.detail(id));
}
如果存在多个参数,前端就继续在url里拼接参数值,注意:每个参数值前面要有"/"
后端会根据"/"按顺序来截取url里的值,并指定参数名称,
const id = 1
const name = ‘麻子’
比如,前端传两个参数并拼接在url里 this.$http.get(’/api/user/detail/’ + id + ‘/’ + name)
后端接收到的请求就是http://localhost:8080/api/user/detail/1/麻子
@GetMapping(value = “/detail1/{id}/{name}”)
public Resp detail1(@PathVariable(“id”) Long id, @PathVariable(“name”) String name) {
return Resp.success(myUserService.detail(id, name));
}

post方式与上面所说的get的方式一致

如果传递的是参数对象,前端调用接口地址 this.$http.get(’/api/user/detail’, 参数对象),需要转成json格式,
后端用@Requstbody接收即可

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值