Solve Cross Origin Resource Sharing with Flask

后端 未结 9 763
面向向阳花
面向向阳花 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条回答
  •  伪装坚强ぢ
    2020-11-28 04:22

    Note: The placement of cross_origin should be right and dependencies are installed. On the client side, ensure to specify kind of data server is consuming. For example application/json or text/html

    For me the code written below did magic

    from flask import Flask,request,jsonify
    from flask_cors import CORS,cross_origin
    app=Flask(__name__)
    CORS(app, support_credentials=True)
    @app.route('/api/test', methods=['POST', 'GET','OPTIONS'])
    @cross_origin(supports_credentials=True)
    def index():
        if(request.method=='POST'):
         some_json=request.get_json()
         return jsonify({"key":some_json})
        else:
            return jsonify({"GET":"GET"})
    
    
    if __name__=="__main__":
        app.run(host='0.0.0.0', port=5000)
    

提交回复
热议问题