我有一个特定的用例,我必须使用
axios.get
获取 pdf 内容,然后在 UI 上显式呈现内容
在前端,我使用 React.js 并在后端使用 Express.js 服务器。
我的前端 React 应用程序无法调用跨域 api(第三方 api),因此,我在后端 express 服务器上创建了一个端点,它运行在最终处理第三方 api 的同一域上,基本上从任何第三方链接获取 pdf 内容.
router.get('/fetch-pdf-content', async (req, res) => {
try {
const { data } = await axios.get('https://abc.pdf');
res.contentType('application/pdf');
res.send(data);
} catch (error) {
console.error(error);
res.status(500).send(error.code);
}
})
在前端,我如何渲染从服务器端点获得的
data
?
我试过在前端使用对象标签
<object data={'http://localhost:5000/fetch-pdf-content'}></object>
但是没用