【问题标题】:CORS policy blocked Cloudfare Worker FunctionCORS 策略阻止 Cloudfare Worker 功能
【发布时间】:2021-12-22 04:54:24
【问题描述】:

我有 cloudfare worker 函数,我试图从我的 React Web 应用程序调用它,但我经常出错

访问获取地址 'https://xxxxx.workers.dev/' 来自原产地 “http://localhost:8000”已被 CORS 策略阻止:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。如果不透明的响应满足您的需求,请设置请求的 模式为“no-cors”以获取禁用 CORS 的资源

我不想使用no-cors 模式,我正在尝试在 cloudfare 仪表板上找到允许来源域的选项,但似乎无法找到。 请问有谁知道如何解决这个问题?

这是函数代码

addEventListener("fetch", (event) => {
  event.respondWith(
    handleRequest(event.request).catch(
      (err) => new Response(err.stack, { status: 500 })
    )
  );
});

/**
 * Many more examples available at:
 *   https://developers.cloudflare.com/workers/examples
 * @param {Request} request
 * @returns {Promise<Response>}
 */
async function handleRequest(request) {
  const { pathname } = new URL(request.url);

  if (pathname.startsWith("/api")) {
    return new Response(JSON.stringify({ pathname }), {
      headers: { "Content-Type": "application/json" },
    });
  }

  if (pathname.startsWith("/status")) {
    const httpStatusCode = Number(pathname.split("/")[2]);

    return Number.isInteger(httpStatusCode)
      ? fetch("https://http.cat/" + httpStatusCode)
      : new Response("That's not a valid HTTP status code.");
  }

  return new Response({
      hello: "world",
    });
}

【问题讨论】:

    标签: cors cloudflare-workers


    【解决方案1】:

    这就是帮助我解决问题的原因:https://community.cloudflare.com/t/worker-site-http-request-to-worker/151529/8

    功能代码:

    // An example worker which supports CORS. It passes GET and HEAD
    // requests through to the origin, but answers OPTIONS and POST
    // requests directly. POST requests must contain a JSON payload,
    // which is simply echoed back.
    
    addEventListener('fetch', event => {
      event.respondWith(handle(event.request)
        // For ease of debugging, we return exception stack
        // traces in response bodies. You are advised to
        // remove this .catch() in production.
        .catch(e => new Response(e.stack, {
          status: 500,
          statusText: "Internal Server Error"
        }))
      )
    })
    
    async function handle(request) {
      if (request.method === "OPTIONS") {
        return handleOptions(request)
      } else if (request.method === "POST") {
        return handlePost(request)
      } else if (request.method === "GET" || request.method == "HEAD") {
        // Pass-through to origin.
        return fetch(request)
      } else {
        return new Response(null, {
          status: 405,
          statusText: "Method Not Allowed",
        })
      }
    }
    
    // We support the GET, POST, HEAD, and OPTIONS methods from any origin,
    // and accept the Content-Type header on requests. These headers must be
    // present on all responses to all CORS requests. In practice, this means
    // all responses to OPTIONS or POST requests.
    const corsHeaders = {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
      "Access-Control-Allow-Headers": "Content-Type",
    }
    
    function handleOptions(request) {
      if (request.headers.get("Origin") !== null &&
        request.headers.get("Access-Control-Request-Method") !== null &&
        request.headers.get("Access-Control-Request-Headers") !== null) {
        // Handle CORS pre-flight request.
        return new Response(null, {
          headers: corsHeaders
        })
      } else {
        // Handle standard OPTIONS request.
        return new Response(null, {
          headers: {
            "Allow": "GET, HEAD, POST, OPTIONS",
          }
        })
      }
    }
    
    async function handlePost(request) {
      if (request.headers.get("Content-Type") !== "application/json") {
        return new Response(null, {
          status: 415,
          statusText: "Unsupported Media Type",
          headers: corsHeaders,
        })
      }
    
      // Detect parse failures by setting `json` to null.
      let json = await request.json().catch(e => null)
      if (json === null) {
        return new Response("JSON parse failure", {
          status: 400,
          statusText: "Bad Request",
          headers: corsHeaders,
        })
      }
    
      return new Response(JSON.stringify(json), {
        headers: {
          "Content-Type": "application/json",
          ...corsHeaders,
        }
      })
    }
    

    拨打电话

    const Http = new XMLHttpRequest();
        const url='HTTPS://YOUR_CLOUDFLARE_WORKER';
        Http.open("POST", url);
        Http.setRequestHeader('Accept', 'application/json');
        Http.setRequestHeader('Content-Type', 'application/json');
        Http.send('{"test": "GP"}');
    
        Http.onreadystatechange = (e) => {
          console.log(Http.responseText)
        }
    

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 2020-09-11
      • 2021-04-16
      • 2018-02-26
      • 2019-11-19
      • 2020-09-12
      • 2022-01-14
      • 2021-02-28
      相关资源
      最近更新 更多