Solve Cross Origin Resource Sharing with Flask

后端 未结 9 764
面向向阳花
面向向阳花 2020-11-28 03:59

For the following ajax post request for Flask (how can I use data posted from ajax in flask?):

$.ajax({
    url: \"http://127.0.0.1         


        
9条回答
  •  旧时难觅i
    2020-11-28 04:14

    It worked like a champ, after bit modification to your code

    # initialization
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'the quick brown fox jumps over the lazy   dog'
    app.config['CORS_HEADERS'] = 'Content-Type'
    
    cors = CORS(app, resources={r"/foo": {"origins": "http://localhost:port"}})
    
    @app.route('/foo', methods=['POST'])
    @cross_origin(origin='localhost',headers=['Content- Type','Authorization'])
    def foo():
        return request.json['inputVar']
    
    if __name__ == '__main__':
       app.run()
    

    I replaced * by localhost. Since as I read in many blogs and posts, you should allow access for specific domain

提交回复
热议问题