Python flask with CORS,Chrome only HTTP POST导致选项请求

2024-05-16 01:39:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python flask和CORS插件构建了一个API。使用GET方法时,跨源请求很好。另外,文章是好的与IE,但不是铬,在那里我看到一个选项的请求,没有后续的帖子。在阅读了大量关于CORS和大多数文档之后,我认为我使用CORS应该处理OPTIONS请求,但不是运气好。在

我的API代码:

        from flask import Flask, Response, jsonify, request
        from flask.ext.cors import CORS, cross_origin #Used to get around the Cross domain request issue (e.g. when making a request to an entity of another origin (ip/domain + port))
        from DB import webAPIInterface
        import json

        DB = webAPIInterface()

        app = Flask(__name__)
        CORS(app)
        app.config['CORS_HEADERS'] = 'Content-Type'



        @app.route('/api/v1.0/alarmArmedState', methods=['POST'])
        @cross_origin() # allow all origins all methods.
        def toggleAlarmArmedState():


            #DO stuff......

注意,我添加了应用程序配置['CORS_HEADERS'。。。以及@cross_origin()在经过大量谷歌搜索之后。好像没什么区别。我在这里也尝试了不同的值,包括JSON的内容类型(这就是我使用的)。在

我还试着加上这个:

^{pr2}$

请告诉我你的想法。有没有什么明显的东西我在处理期权调用时遗漏了?在


Tags: tofromimportapiappflaskdbrequest