简体   繁体   English

尝试从前端调用 Google Cloud function 时出现 CORS 错误(网站)

[英]CORS error when trying to call Google Cloud function from the front-end (Web site)

I have a CORS issue with my google cloud function when I try to call it from a Web site (front-end).当我尝试从 Web 站点(前端)调用它时,我的谷歌云 function 出现 CORS 问题。 The problem is that I can succesfully execute the same code in Jupyter notebook without error.问题是我可以在 Jupyter notebook 中成功执行相同的代码而不会出错。 I've added so far all what is required to enable CORS access but I'm still getting the same error (Access to XMLHttpRequest at 'https://****' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.. Any help please?到目前为止,我已经添加了启用 CORS 访问所需的所有内容,但我仍然遇到相同的错误(从来源“http://localhost:3000”访问“https://****”处的 XMLHttpRequest已被 CORS 策略阻止:所请求的资源上不存在“访问控制允许来源”header。有什么帮助吗?

Below is the source code:下面是源代码:

import requests
import json
import flask
def myFunction(request):
    request_json = request.get_json()
    # preflight request
    if request.method == 'OPTIONS':
        headers = {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        }

        print ('', 204, headers)

    # main request
    headers = {
        'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
        'Access-Control-Allow-Origin': '*'
    }
#    Call to get a access token from a refresh token
    response = requests.post(url = "https://accounts.zoho.com/oauth/v2/...",headers = headers)
    accessToken = response.json()['access_token']
    # Call to zoho Api to insert a new lead
    url = 'https://www.zohoapis.com/crm/v2/Leads'
    v_access = 'Zoho-oauthtoken ' + accessToken 
    headers = {
            'Authorization': v_access,
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,GET, POST'
        }
    json_data = request.get_json(silent=True)
    newdata = json.dumps(json_data)
    v_data = str(json.loads(newdata))
    v_data1 = v_data.encode('utf-8')
    post_response = requests.post(url,headers = headers, data = v_data1)
    post_response.headers['Access-Control-Allow-Origin'] = '*'
    post_response.headers['Access-Control-Allow-Methods'] = 'GET, POST'
    return (post_response.status_code)

You don't normally want to get bogged down handling CORS headers manually, try a middleware such as https://flask-cors.readthedocs.io/en/latest/#route-specific-cors-via-decorator您通常不想陷入手动处理 CORS 标头的困境,请尝试使用中间件,例如https://flask-cors.readthedocs.io/en/latest/#route-specific-cors-via-decorator

Specifically around your response though, you never actually return headers, just a status code.但是,特别是围绕您的响应,您实际上从未返回标头,只是一个状态代码。

If you see the example on GCP you can see you need ACAO = * header set in the response.如果您在 GCP 上看到示例,您会发现您需要在响应中设置 ACAO = * header。 https://cloud.google.com/functions/docs/writing/http#preflight_request https://cloud.google.com/functions/docs/writing/http#preflight_request

# Set CORS headers for the main request
headers = {
    'Access-Control-Allow-Origin': '*'
}

return ('Hello World!', 200, headers) // Need the Headers in the Response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从前端应用调用谷歌云 Function - Call Google Cloud Function from front-end application 使用谷歌云从前端下载文档 Function - Download documents from the front end using a Google Cloud Function Google Cloud Function Cors 仅在引发错误时出错 - Google Cloud Function Cors Error Only When Error is Thrown Firebase 云 function cors 即使添加 cors 中间件也会出错 - Firebase cloud function cors error even when adding cors middleware 从云调度程序调用谷歌云 function 时获取权限被拒绝错误 - Getting permission denied error when calling Google cloud function from Cloud scheduler 在通过后端生成和发送这些登录链接时,防止前端生成 email 登录链接 - Prevent front-end generated email sign-in links when generating and sending these via backend 为什么我在尝试从本地谷歌云 function 连接到谷歌云 MySQL db 时收到 sqlalchemy.exc.OperationalError? - Why am I getting sqlalchemy.exc.OperationalError when trying to connect to Google Cloud MySQL db from a local google cloud function? 如何在 Google Cloud 上部署 Flask 后端和 React 前端 - How to deploy a Flask Backend and React Front End on Google Cloud 更新 Google Kube.netes 引擎 Web 应用程序的 Web 前端 - Update the Web-Front-End of a Google Kubernetes Engine Webapplication 从 Google Cloud function 中调用 shell 脚本 - Call shell script from within Google Cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM