谷歌云函数python CORS错误号';访问控制允许原点';请求的资源上存在标头。
我遵守了学校的指示 所以我的代码在结尾有这个谷歌云函数python CORS错误号';访问控制允许原点';请求的资源上存在标头。,python,cors,google-cloud-functions,Python,Cors,Google Cloud Functions,我遵守了学校的指示 所以我的代码在结尾有这个 # Set CORS headers for the preflight request if request.method == 'OPTIONS': # Allows GET requests from any origin with the Content-Type # header and caches preflight response for an 3600s
# Set CORS headers for the preflight request
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# Set CORS headers for the main request
headers = {
'Content-Type':'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
# END CORS
return (res, 200, headers)
其中res是JSON
通过一个简单的节点应用程序,我通过
this.http.post(payloadTarget.url, JSON.stringify(clone)).subscribe(res => {
console.log('request complete', res);
},
err => {
console.log('request failed', err);
});
我在控制台上收到这个错误
请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”
当使用Postman测试帖子或选项时,我看不到错误,但我也看不到应该显示的标题
我相信这很简单,但看看其他类似的问题和答案,却找不到任何指向我的问题的东西我认为问题在于你在选项回复中遗漏了
帖子
。对于CORS,您需要明确哪些http方法是可接受的。话虽如此,您需要将您的飞行前请求更新为:
# Set CORS headers for the preflight request
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# Set CORS headers for the main request
headers = {
'Content-Type':'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
}
# END CORS
return (res, 200, headers)
或者,可以将该值设置为:
'Access-Control-Allow-Methods': '*'
如果你不那么关心安全
此外,您没有通过邮递员收到这些错误的原因是由于请求的性质-所有现代浏览器都会截获您的请求并检查来源是否匹配(域、端口、协议等),如果不匹配,则会向目的地发出飞行前(
选项
)请求,以确保其正常。POSTMAN之类的网站和Fiddler之类的软件不是从浏览器启动的,而是在不进行任何检查的情况下提交的。您可以创建一个功能,将COR添加到每个响应中。使用jsonify将dict强制转换为JSON
from flask import jsonify
def addCors(response, code=200):
headers = {'Access-Control-Allow-Origin': '*'}
return (response, code, headers)
def func_with_cors(request):
response = jsonify({"key1":"value1","key2":"value2"})
return addCors(response)
然后用cors部署func_
gcloud函数使用cors部署func_——运行时python37——触发http
当我给函数添加注释时,就会发生这种情况。我添加了注释,因为云函数不显示Python的详细错误日志。当我删除批注时,我的函数将返回预期的标题。仍然不起作用,添加了“访问控制允许方法”:“*”对于Bot我刚刚尝试复制了它,它对我有效,您确定已部署最新版本的函数吗?使用
curl
发送OPTIONS
请求时会得到什么curl-i-X选项
Hi@Dustin,我使用gcloud函数部署functionName--runtime python37--trigger http
进行部署,curl返回http/2200内容类型:text/html;charset=utf-8函数执行id:ms2z3dcwv1tz x-cloud-trace-context:D4B120A314D0D4A2BB8B7F57D0F5BA17;o=1日期:2018年11月1日星期四07:47:38 GMT服务器:谷歌前端内容长度:2 alt svc:quic=“:443”;ma=2592000;v=“44,43,39,35”
我也遇到了同样的问题。尝试使用curl-i-X选项
,我得到访问控制允许标题:内容类型访问控制允许方法:POST,选项访问控制允许来源:*访问控制最大年龄:3600内容类型:text/html;charset=utf-8
与Chrome不兼容。