vue中发送post请求
时间: 2023-11-12 14:48:09 浏览: 100
在Vue中发送POST请求可以使用Vue Resource或者Axios库。如果使用Vue Resource,你需要在HTML文件中引入vue-resource.min.js的脚本文件。然后可以使用`this.$http.post`方法发送POST请求。如果使用Axios库,你需要在HTML文件中引入axios的脚本文件。然后可以使用`axios.post`方法发送POST请求。
相关问题
vue怎么发送post请求
在Vue中发送POST请求可以使用Vue的内置方法`axios.post`,具体实现如下:
```
axios.post(url, data).then(response => {
console.log(response.data)
}).catch(error => {
console.log(error)
})
```
其中,`url`为请求的地址,`data`为需要发送的数据。`axios.post`方法返回一个Promise对象,我们可以通过`.then`方法获取到请求成功后返回的数据,通过`.catch`方法获取到请求失败后的错误信息。需要注意的是,在使用`axios`发送POST请求时,如果需要发送JSON格式的数据,需要在请求头中设置`Content-Type`为`application/json`,如下所示:
```
axios.post(url, data, {
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
console.log(response.data)
}).catch(error => {
console.log(error)
})
```
vue axios发送post请求
可以使用以下代码发送post请求:
```
axios.post(url, data)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
```
其中,url是请求的地址,data是要发送的数据。发送成功后,会在控制台输出响应的数据。
阅读全文
相关推荐















