温馨提示×

axios返回结果如何看请求体

小亿
142
2024-01-22 15:38:04
栏目: 编程语言

在使用axios发送请求时,可以通过以下方式来查看请求体:

  1. 使用.then()来处理请求成功的回调函数,在回调函数中打印出返回结果:
axios.post('/api/endpoint', { data: 'example' }) .then(response => { console.log(response.data); // 输出返回结果 }) .catch(error => { console.error(error); }); 
  1. 使用async/await的方式处理请求,并在控制台打印出结果:
async function fetchData() { try { const response = await axios.post('/api/endpoint', { data: 'example' }); console.log(response.data); // 输出返回结果 } catch (error) { console.error(error); } } fetchData(); 

无论是通过.then()方法还是通过async/await的方式处理返回结果,都可以使用response.data来访问请求返回的数据。

0