Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 Falcon和Axios:无法允许CORS_Python_Reactjs_Axios_Falcon - Fatal编程技术网

Python Falcon和Axios:无法允许CORS

Python Falcon和Axios:无法允许CORS,python,reactjs,axios,falcon,Python,Reactjs,Axios,Falcon,我很难允许CORS请求到Flask服务器。客户端正在使用axios进行响应。客户端上的错误为: Access to XMLHttpRequest at <url> has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 2通过中间件在全球范围内使用falcon_cors: from wsgiref.simple_ser

我很难允许CORS请求到Flask服务器。客户端正在使用axios进行响应。客户端上的错误为:

Access to XMLHttpRequest at <url> has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
2通过中间件在全球范围内使用falcon_cors:

from wsgiref.simple_server import make_server
import falcon
from falcon_cors import CORS    
from flask import jsonify
import transform
import json
import engine


cors = CORS(allow_origins_list=[
    '<client ip>'
    ])

index = transform.reindex()
app = falcon.API(middleware=[cors.middleware])


class Search:
    def on_get(self, request, response):
        query = request.params['searchText']
        result = engine.search(query, index)


        response.status = falcon.HTTP_200
        response.body = json.dumps(result)


search = Search()

app.add_route('/search', search)

if __name__ == '__main__':
    with make_server('', 8003, app) as httpd:
        print('Serving on port 8003...')
        httpd.serve_forever()
什么都没用。当我在浏览器中检查响应时,我可以看到“access control allow origin”:“*”我在某个地方读到axios无法始终看到所有标题。以前有人遇到过这种情况吗?谢谢。

可能的情况-

使用浏览器时,如果服务器要求,它会在您的请求中附加凭据:true。但当涉及角度或反应时,您必须明确提供凭据:在您的httpOptions中为true

我建议你用猎鹰或烧瓶。 如果只是在gunicorn或女服务员的指导下使用Falcon,以下程序可能会有所帮助-

得到

有一些白名单上的方法

了解更多关于

搜索类如下所示

class Search:

    def on_get(self, req, resp):
        response_obj = {
            "status": "success"
        }
        resp.media = response_obj
有一些白名单上的起源

现在,您可以将路由添加到类中

from src.search import SearchResource
api.add_route('/search', SearchResource())
作为参考,如果传入请求中存在withCredentials:true,请确保您不会错过上面cors中设置为“白名单”的“允许\ U凭据\来源\列表”

如果要允许凭据,则不应将allow_all_origins设置为True。您必须在允许\u凭据\u来源\u列表中指定确切的协议+域+端口

from falcon_cors import CORS
# Methods supported by falcon 2.0.0
# 'CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE'

whitelisted_methods = [
    "GET",
    "PUT",
    "POST",
    "PATCH",
    "OPTIONS" # this is required for preflight request
]
class Search:

    def on_get(self, req, resp):
        response_obj = {
            "status": "success"
        }
        resp.media = response_obj
whitelisted_origins = [
    "http://localhost:4200",
    "https://<your-site>.com"
]
cors = CORS(
    # allow_all_origins=False,
    allow_origins_list=whitelisted_origins,
    # allow_origins_regex=None,
    # allow_credentials_all_origins=True,
    # allow_credentials_origins_list=whitelisted_origins,
    # allow_credentials_origins_regex=None,
    allow_all_headers=True,
    # allow_headers_list=[],
    # allow_headers_regex=None,
    # expose_headers_list=[],
    # allow_all_methods=True,
    allow_methods_list=whitelisted_methods
)

api = falcon.API(middleware=[
    cors.middleware,
    # AuthMiddleware()
    # MultipartMiddleware(),
])
from src.search import SearchResource
api.add_route('/search', SearchResource())