简体   繁体   English

瓶子 PY:响应预检请求,无“访问控制允许来源”header

[英]Bottle PY: Response to preflight request, No 'Access-Control-Allow-Origin' header

I am developing a little web app and on my end everything works fine, but when my colleague tries it out on his end the gets this CORS error:我正在开发一个小 web 应用程序,在我这边一切正常,但是当我的同事在他这边尝试时,得到这个 CORS 错误:

Access to XMLHttpRequest at 'http://localhost:3333/registerPID/' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The server is running on localhost:3333 and is written in Python using the Bottle Framework.服务器在 localhost:3333 上运行,并使用 Bottle 框架以 Python 编写。 The browser sends a POST Request with some user data to the server and the server then responds with a PID.浏览器向服务器发送带有一些用户数据的 POST 请求,然后服务器以 PID 响应。 I also added the CORS headers to the response but it still doesn't seem to work for him: Decorator:我还在响应中添加了 CORS 标头,但它似乎仍然对他不起作用:装饰器:

def enable_cors(fn):
        def _enable_cors(*args, **kwargs):
            # set CORS headers
            response.headers['Access-Control-Allow-Origin'] = '*'
            response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
            response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

            if bottle.request.method != 'OPTIONS':
                # actual request; reply with the actual response
                return fn(*args, **kwargs)
        return _enable_cors

Route for the POST request (formatting is off on here, is correct in the code): POST 请求的路由(格式在此处关闭,在代码中是正确的):

@route("/registerPID/", method='POST')
@enable_cors
    def register_PID():
        register_data = request.body.getvalue().decode('utf-8')
        register_values = []
        register_values.append(register_data.split(','))
        print(register_values[0][3])

        with open('pidList.csv', newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            maxID = -1

            for row in reader:
                maxID += 1
                print(register_values[0][3])
                if row['uniqueBrowserID'] == register_values[0][3]:
                    if row['hasFinished'] == str(0):
                        failed_pid = row['ID']
                        for x in range(6):
                            file_path = log_path + failed_pid + '-' + str(x) + ".csv"
                            if os.path.isfile(file_path):
                                shutil.move(file_path,failed_log_path + failed_pid + '-' + str(x) + ".csv")
                        print("re play: " + str(maxID))
                        return str(maxID)
                    else:
                        return "f1"
            with open("pidList.csv", "a") as csvfile:
                id = maxID + 1
                writer = csv.writer(csvfile, delimiter=',')
                writer.writerow([register_values[0][0],register_values[0][1],register_values[0][2],id,0,register_values[0][3],register_values[0][4]])
                return str(id)

Browser Ajax Call:浏览器 Ajax 调用:

function getPidCall(formCsvData) {
    $.ajax({
        url: "http://localhost:3333/registerPID/",
        type: "POST",
        data: formCsvData,
        contentType: "text",
        dataType: "text",
        async: true,
        success: function (response) {
            pid = response;
            if (isNaN(response)) {
                hasAlreadyParticipated = true
                pid = 0
            }
            removeStartScreeen()
            startScreenIsActive = false
            setupScene()
            requestLock();
        }
    });

}

How do I fix this?我该如何解决?

My guess is that you've failed to include OPTIONS in your method list.我的猜测是您未能在方法列表中包含OPTIONS Does this work any better:这是否工作得更好:

@route("/registerPID/", method=['POST', 'OPTIONS'])
@enable_cors
    def register_PID():
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “请求 header 字段 Access-Control-Allow-Origin 在预检响应中被 Access-Control-Allow-Headers 不允许”尽管 CORS 配置有效 - “Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response” despite valid CORS config 请求的资源上不存在“Access-Control-Allow-Origin”标头。 或 对预检请求的响应未通过访问控制检查 - No 'Access-Control-Allow-Origin' header is present on the requested resource. OR Response to preflight request doesn't pass access control check Bottle.py-“所请求的资源上没有'Access-Control-Allow-Origin'标头” - Bottle.py - “No 'Access-Control-Allow-Origin' header is present on the requested resource” 设置Access-Control-Allow-Origin标头时如何绕过预检检查? - How to bypass preflight check while setting Access-Control-Allow-Origin header? Grafana 与 Bottle 对话时没有 Access-Control-Allow-Origin - No Access-Control-Allow-Origin when Grafana talks to Bottle 授权 Firebase 请求 Cloud Functions HTTP 返回预检请求未通过控制检查(无访问控制允许来源) - Authorized Firebase request to Cloud Functions HTTP return preflight request does not pass control check (No Access-Control-Allow-Origin) 飞行前响应中的Access-Control-Allow-Headers不允许请求标头字段Access-Control-Request-Methods - Request header field Access-Control-Request-Methods is not allowed by Access-Control-Allow-Headers in preflight response 由于没有 Access-Control-Allow-Origin 标头但标头存在而被 CORS 阻止的 HTTP 请求 - HTTP request blocked by CORS for not having Access-Control-Allow-Origin header but the header is present Axios blocked by CORS policy with Django REST Framework: 'Access-Control-Allow-Origin' header in the response must not be wildcard - Axios blocked by CORS policy with Django REST Framework: 'Access-Control-Allow-Origin' header in the response must not be wildcard Flask-socketio-未能设置“ Access-Control-Allow-Origin”响应头 - Flask-socketio - failed to set “Access-Control-Allow-Origin” response header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM