要使用 Axios 从 API 返回 HTML,可以按照以下步骤进行操作:
import axios from 'axios';
axios.get('https://api.example.com/html')
.then(response => {
// 在这里处理 API 返回的 HTML
const html = response.data;
console.log(html);
})
.catch(error => {
console.error(error);
});
.then 回调函数中,可以访问 response.data 来获取 API 返回的 HTML。可以在这里对 HTML 进行进一步的处理,比如渲染到页面上。注意:Axios 默认情况下会自动将响应数据解析为 JSON,因此需要设置响应类型为 'text/html',以确保返回的数据被解析为 HTML。
axios.get('https://api.example.com/html', { responseType: 'text/html' })
.then(response => {
// 在这里处理 API 返回的 HTML
const html = response.data;
console.log(html);
})
.catch(error => {
console.error(error);
});
这样,你就可以使用 Axios 从 API 返回 HTML 并进行相应的处理了。