node.js - 对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。- 谷歌云功能,Vue3
问题描述
我正在尝试使用 Google Cloud Functions 制作网络爬虫。当在 GCF 测试功能区域输入输入时,GCF 按预期工作,但是当使用来自客户端的 HTTP 请求(在 Vue3 中编码)触发 GCF 时,我收到以下 CORS 错误:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。
这是我来自客户端的 HTTP 请求,
const response = await fetch(url, {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
"Allow-Control-Allow-Origin": "*",
},
body: JSON.stringify({ input }),
});
// console.log(response);
const data = await response.json();
GCF 代码
const puppeteer = require("puppeteer");
exports.webScrape = async (req, resp) => {
resp.header("Access-Control-Allow-Origin", "*");
resp.header("Access-Control-Allow-Credentials", "true");
resp.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
resp.header(
"Access-Control-Allow-Headers",
"Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"
);
const input = req.body.input;
async function scrapeSite(url) {
//it does stuff here
}
try {
const response = await scrapeSite(input);
const data = await JSON.stringify(response);
resp.send(data);
} catch (error) {
console.log("server is catching the error");
console.log(error);
resp.status(404).send({
ErrorMessage: "An invalid URL was given to the input box, please check the input and try again",
error,
});
}
return;
};
解决方案
推荐阅读
- c++ - 使用 mysql-cpp-connector 连接到本地数据库
- reactjs - 每次按下键时,React re 都会渲染所有内容
- python - 使用python获取只读excel
- java - 如何使用 application.properties 排除 packageScanning?
- python - 使用 python 脚本激活 python venv
- sql - ORDS:从 ORDS OUT 参数中的 json 中删除转义字符
- python - 每次我运行我的文本文件程序时,我都会得到 runfile wdir 输出
- angularjs - 如何在 Angular 9 中使用引导程序 4?
- javascript - 如何生成一个对象,捕获用户的 id 并放入我的结果?
- php - 如何在 laravel 中转换特定的 fromat