Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 了解如何使用CORS获取云函数_Python 3.x_Python Requests_Cors_Google Cloud Functions_Fetch - Fatal编程技术网

Python 3.x 了解如何使用CORS获取云函数

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

我已经部署了中所示的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.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时得到完全相同的结果是什么意思