axios post请求springboot获取不到参数值的配置

vue main.js配置 


import axios from 'axios'
import qs from 'qs'
axios.defaults.baseURL='http://localhost:8090/api/'


axios.interceptors.request.use(config=>{
    config.headers.Authorization = window.sessionStorage.getItem('token');
    if(config.method=="post"){
      config.data = qs.stringify(config.data);
      config.headers["Content-Type"] = "application/x-www-form-urlencoded";
      console.log(config);
    }
    return config;
})

 

vue页面请求设置

 

//post请求设置
//const {data:res} = await this.$axios.post('login',this.user);
// console.log(res);

//get请求设置
const {data:res} = await this.$axios.get('getUser?username='+this.user.username);
console.log(res);

 

### Axios Post 请求中 Data 参数的意义和作用 在 AxiosPost 请求中,`data` 参数是用于请求体(Request Body)的核心部分。它定义了客户端向服务器发送的数据内容,具体作用如下: 1. **数据输**:`data` 参数承载了需要发送到服务器的数据[^1]。这些数据可以是简单的键值对、数组、对象,甚至是复杂的 JSON 结构。 2. **格式支持**:根据服务器的接收方式,`data` 参数可以以不同的格式发送,例如 `application/json`、`application/x-www-form-urlencoded` 或 `multipart/form-data` 等[^5]。 3. **与后端交互**:`data` 参数的内容会被序列化为指定的格式,并通过 HTTP 请求体发送给服务器。后端框架(如 Gin 框架或 SpringBoot)会解析该请求体并映射到相应的参数或实体对象[^3]。 4. **动态配置**:在实际开发中,可以通过拦截器或手动设置请求头(如 `Content-Type`),来控制 `data` 参数的输格式[^4]。例如,如果需要发送表单数据,可以将 `Content-Type` 设置为 `application/x-www-form-urlencoded` 并使用 URL 编码的字符串作为 `data` 参数[^2]。 以下是一个使用 `data` 参数发送 JSON 数据的示例: ```javascript axios.post('/api/data', { key: 'value', array: [1, 2, 3] }, { headers: { 'Content-Type': 'application/json' } }).then(response => console.log(response)) .catch(error => console.error(error)); ``` 如果需要发送表单数据,可以调整 `data` 参数和请求头: ```javascript axios.post('/api/data', 'key=value&array[]=1&array[]=2&array[]=3', { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(response => console.log(response)) .catch(error => console.error(error)); ``` ### 注意事项 - 如果未正确设置 `Content-Type`,可能导致后端无法正确解析请求体中的数据[^3]。 - 在某些情况下,`data` 参数可能为空,表示不需要向服务器递额外的数据[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值