小编lsp*_*iro的帖子

对获取“url”的访问已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。ReactJS

我正在尝试使用以下代码从开发模式下的 React 应用程序获取无服务器函数。

let response = await fetch(url,
       {
           method: 'POST',
           mode: 'cors',
           body: "param=" + paramVar,
        })
        .then(response => response.json());
Run Code Online (Sandbox Code Playgroud)

后端函数是一个Python Cloud函数,代码如下:

def main(request):
    # CORS handling
    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', …
Run Code Online (Sandbox Code Playgroud)

javascript python reactjs fetch-api google-cloud-functions

20
推荐指数
1
解决办法
8万
查看次数