axios异步访问后台 @RequestParam 获取参数 HTTP Status 400 - Required String parameter 'xx' is not present...

axios 异步请求三种方式

1、Content-Type: application/json

后台使用@RequestBody

获取参数

import axios from 'axios'
let data = {code:'123',name:'yyyy'};
axios.post(`${this.$url}/test/testRequest`,data)
.then(res=>{
    console.log('res=>',res);            
})

2、Content-Type: multipart/form-data

后台使用HttpServletRequest request 

获取参数

import axios from 'axios'
let data = new FormData();
data.append('code','1234');
data.append('name','yyyy');
axios.post(`${this.$url}/test/testRequest`,data)
.then(res=>{
    console.log('res=>',res);            
})

3、Content-Type: application/x-www-form-urlencoded

import axios from 'axios'
import qs from 'Qs'
let data = {'code':'234','name':'yyyy'};
axios.post(`${this.$url}/testRequest`,qs.stringify({
    data
}))
.then(res=>{
    console.log('res=>',res);            
})

后台接收参数写法

@PostMapping("testRequest")
public String resetPwd( String code, String name) {
        System.out.println(code);
        return "xxx";
    }

如果使用@RequestParam 则需要这样写

  @PostMapping("testRequest")
    public String testRequest(@RequestParam(value = "code",required = false) String code, @RequestParam(value = "name", required = false) String name) {
        System.out.println(code);
        return "dfdfdfd";
    }

总结:
application/x-www-form-urlencoded请求是表单请求,可以用@RequestParam一个一个获取参数,当Content-Type == application/json 前端传来的是json串,用@RequestParam是获取不到的,需要用@RequestBody将json串华为对

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值