首页 > 解决方案 > 对预检请求的响应未通过访问控制检查:它没有 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;
};

标签: node.jsvue.jsgoogle-cloud-functions

解决方案


推荐阅读