vue axios使用post
时间: 2023-09-02 14:11:30 浏览: 165
vue axios数据请求get、post方法及实例详解
在Vue中使用axios发送POST请求的示例代码如下:
```
axios({
url: '/your/api/endpoint',
method: 'post',
data: {
// 请求体的参数
},
transformRequest: [function (data) {
let formData = new FormData();
for (let key in data) {
formData.append(key, data[key]);
}
return formData;
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
```
以上代码中,你需要替换`/your/api/endpoint`为你的API接口的URL。在`data`中可以设置请求体的参数。通过`transformRequest`,我们将参数转换为`FormData`对象,以满足以`application/x-www-form-urlencoded`格式发送POST请求的要求。请求头中也需要设置`'Content-Type': 'application/x-www-form-urlencoded'`。你可以根据实际情况修改这些代码来适应你的项目需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [vue中使用axios发送post请求](https://blog.csdn.net/weixin_44432032/article/details/108560136)[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: 33.333333333333336%"]
- *2* [vue中使用axios插件做post请求带参数](https://blog.csdn.net/allen_csdns/article/details/103296618)[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: 33.333333333333336%"]
- *3* [ChatGPT聊天微信小程序源码适配H5和WEB端.zip](https://download.csdn.net/download/m0_66047725/88222777)[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: 33.333333333333336%"]
[ .reference_list ]
阅读全文