How do you interpret the request body in the axios response?
When using axios to send a request, you can view the request body in the following way:
- Handle successful callbacks by using .then(), and print the returned result in the callback function.
axios.post('/api/endpoint', { data: 'example' })
.then(response => {
console.log(response.data); // 输出返回结果
})
.catch(error => {
console.error(error);
});
- Handle requests using async/await and print the results in the console.
async function fetchData() {
try {
const response = await axios.post('/api/endpoint', { data: 'example' });
console.log(response.data); // 输出返回结果
} catch (error) {
console.error(error);
}
}
fetchData();
Whether you handle the returned results through the .then() method or using async/await, you can access the data returned from the request using response.data.