[英]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.