Python 3.x 了解如何使用CORS获取云函数
我已经部署了中所示的Python云函数 当我从浏览器中获取端点时,如下所示:Python 3.x 了解如何使用CORS获取云函数,python-3.x,python-requests,cors,google-cloud-functions,fetch,Python 3.x,Python Requests,Cors,Google Cloud Functions,Fetch,我已经部署了中所示的Python云函数 当我从浏览器中获取端点时,如下所示: fetch('https://this-is-a-secret.cloudfunctions.net/cors-enabled-function', {method: 'GET'}).then(response => console.log(response)) 我得到以下响应对象: Response {type: "cors", url: "https://this-is-a-secret.cloudfunc
fetch('https://this-is-a-secret.cloudfunctions.net/cors-enabled-function',
{method: 'GET'}).then(response => console.log(response))
我得到以下响应
对象:
Response {type: "cors", url: "https://this-is-a-secret.cloudfunctions.net/cors-enabled-function", redirected: false, status: 200, ok: true, …}
type: "cors"
url: "https://this-is-a-secret.cloudfunctions.net/cors-enabled-function"
redirected: false
status: 200
ok: true
statusText: ""
headers: Headers {}
body: (...)
bodyUsed: false
__proto__: Response
如果我也这样做,但是使用POST
方法,我会得到完全相同的结果。如果我将方法更改为PUT
,那么它会抛出一个错误,这是预期的,因为我只允许GET
方法
我的问题如下:
为什么使用POST
提取时未显示相同的错误?如果您设置:
if request.method == 'GET':
1.运行'curl-X POST或PUT url并检查'gcloud functions logs read'将看到:
Function execution took 9 ms, finished with status code: 200
Function execution took 13 ms, finished with status code: 204
如果您检查curl-v
,您将看到标题:
access-control-allow-origin: *
< access-control-allow-headers: Content-Type
< access-control-allow-methods: GET
< access-control-allow-origin: *
< access-control-max-age: 3600
2.运行“curl-X GET url”并检查“gcloud函数日志读取”将看到:
Function execution took 9 ms, finished with status code: 200
Function execution took 13 ms, finished with status code: 204
如果您检查curl-v
,您将看到标题:
access-control-allow-origin: *
< access-control-allow-headers: Content-Type
< access-control-allow-methods: GET
< access-control-allow-origin: *
< access-control-max-age: 3600
因此,我不明白在运行GET和POST时得到完全相同的结果是什么意思