vue axios.post
时间: 2023-05-29 08:03:56 浏览: 150
vue axios数据请求get、post方法及实例详解
Axios is a popular JavaScript library used to make HTTP requests. The `axios.post` method is used to send a POST request to a specified endpoint.
Here is an example of using `axios.post` in a Vue component:
```javascript
import axios from 'axios';
export default {
methods: {
submitForm() {
const formData = {
name: this.name,
email: this.email,
message: this.message
};
axios.post('/api/contact', formData)
.then(response => {
console.log(response.data);
// handle successful response
})
.catch(error => {
console.error(error.response.data);
// handle error response
});
}
}
}
```
In this example, we define a `submitForm` method that creates an object with form data and sends a POST request to `/api/contact` with that data. The `then` method handles the successful response, while the `catch` method handles errors. The `response.data` property contains the data returned from the server.
阅读全文