javascript - 通过 Firebase 获取 html 到 React 时解析错误
问题描述
我正在尝试使用 Firebase 创建一个云函数端点,我可以从中获取 html 作为字符串。“惊人”的想法是在带有 fetch 的 React 组件中显示 HTML 页面。这是 Firebase 快速实现:
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({origin: true});
const app = express();
app.get('/page3', (request, response) => {
cors(request, response, () => {
response.send(
'<div><p>Det är en sida</p><section><h1>Return of the King</h1><h2><span>A Long-Expected Party</span></h2><p><br></br></p><p>When Mr. Bilbo Baggins of Bag End announced</p><p>Bilbo was very rich and for fame, wealth.</p><p>‘It will have to be paid for,’ they said. ‘It isn’t natural, and trouble will come of it!’</p><p><br></br></p><p><br></br></p><p><span style="background-color: rgb(255, 255, 0);">Note: Look at this link to understand more</span>: <a href="www.svt.se" target="_blank">svt.se</a></p><p><br></br></p><p><br></br></p><p>‘A very nice well-spoken gentlehobbit is Mr. Bilbo, as I’ve always said,’ the Gaffer declared. With perfect truth: for Bilbo was very polite to him.</p><p>‘But what about this Frodo that lives with him?’ asked Old Noakes of Bywater. ‘Baggins is his name, but he’s more than half a Brandybuck, they say.’</p><p><br></br></p><p>‘And and right agin the Old Forest.’</p><p>‘Very much like Mr. Bilbo, Baggins; there was never much to tell of him, till he was drownded.’</p><p><br></br></p></section></div>'
)
});
})
exports.app = functions.https.onRequest(app);
在 React 应用程序中,我只想获取并解析端点提供的 html 字符串,然后尝试记录它。我得到的错误是:
<parseerror style= "display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black">
<h3> This page contains the following errors: </h3>
<div style="font-family:monospace;font-size:12px"> error on line 1 at column 1: Extra content at the end of the document
</div>
之后在 Chrome 中出现黄色警告:
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://easye-9364e.firebaseapp... with MIME type text/html. See https://www.chromestatus.com/feature/56 for more details.
这里有什么问题?
解决方案
你错过了这条线:
app.use(cors);
前app.get()
所以你的代码应该是这样的:
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({origin: true});
const app = express();
app.use(cors);
app.get('/page3', (request, response) => {
cors(request, response, () => {
response.send(
'<div><p>Det är en sida</p><section><h1>Return of the King</h1><h2><span>A Long-Expected Party</span></h2><p><br></br></p><p>When Mr. Bilbo Baggins of Bag End announced</p><p>Bilbo was very rich and for fame, wealth.</p><p>‘It will have to be paid for,’ they said. ‘It isn’t natural, and trouble will come of it!’</p><p><br></br></p><p><br></br></p><p><span style="background-color: rgb(255, 255, 0);">Note: Look at this link to understand more</span>: <a href="www.svt.se" target="_blank">svt.se</a></p><p><br></br></p><p><br></br></p><p>‘A very nice well-spoken gentlehobbit is Mr. Bilbo, as I’ve always said,’ the Gaffer declared. With perfect truth: for Bilbo was very polite to him.</p><p>‘But what about this Frodo that lives with him?’ asked Old Noakes of Bywater. ‘Baggins is his name, but he’s more than half a Brandybuck, they say.’</p><p><br></br></p><p>‘And and right agin the Old Forest.’</p><p>‘Very much like Mr. Bilbo, Baggins; there was never much to tell of him, till he was drownded.’</p><p><br></br></p></section></div>'
)
});
})
exports.app = functions.https.onRequest(app);
推荐阅读
- python - 使用 pyqt5 在 python 上遇到一些错误
- django - 多个子模型的嵌套序列化器
- go - 有没有更好的方法来列出属于特定 K8s 对象的 K8s 事件?
- c - 如何检查是否在 libpthread.so.0 中禁用了硬件锁省略 (HLE)?
- javascript - 在 Reactjs 中使用带有钩子的 map 函数时出错
- mysql - 我无法从选择子查询中获取任何值
- api - 为什么虽然声明了linkedin grant_type?
- javascript - react.js indexDB大数据store.put性能问题
- php - 如何在另一个数组中插入关联数组?PHP
- oracle - C#/Dapper 将 XML CLOB 传递给 Oracle 存储过程导致 LPX-00210