axios post写法
时间: 2023-09-21 12:03:58 浏览: 105
在使用axios进行POST请求时,可以有两种不同的写法。第一种是使用application/json作为Content-Type,将数据作为JSON对象发送。代码示例如下:
```javascript
import axios from 'axios';
let data = {"code":"1234","name":"yyyy"};
axios.post(`${this.$url}/test/testRequest`,data)
.then(res => {
console.log('res=>', res);
});
```
第二种是使用application/x-www-form-urlencoded作为Content-Type,将数据进行URL编码后发送。需要使用qs库来进行数据的编码。代码示例如下:
```javascript
import axios from 'axios';
import qs from 'Qs';
let data = {"code":"1234","name":"yyyy"};
axios.post(`${this.$url}/test/testRequest`,qs.stringify({ data }))
.then(res => {
console.log('res=>', res);
});
```
根据具体需求和后端接口的要求,选择适合的写法即可。在前后端联调过程中,需要注意发送的内容格式,避免出现不必要的问题和错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [axios POST提交数据的三种请求方式写法](https://blog.csdn.net/weixin_42312074/article/details/105707079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [Vue-axios-post数据后端接不到问题解决](https://download.csdn.net/download/weixin_38526751/13203209)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文