When

cross domain file upload, the browser will automatically initiate a OPTIONS method to solve the front-end server, now the background of OPTIONS cross domain to solve the problem of

front-end client initiated cross domain requests can be said to be a "pre request" for subsequent detection really need cross domain POST initiated request is whether safe accept for a server, because cross domain data submitted to the server can be there is a big security problem of

Access-Control-Request-Method server in the request header to remind the next request will make the request

Access-Control-Allow-Method and Access-Control-Allow-Origin respectively to inform the client using what kind of method, the server allows the client for cross domain method and domain name

sample

download flask_cors package < / P>

 PIP install flask-cors

flask_cors CORS

 from flask_cors code example import * app = Flask (__name__) CORS (APP, supports_credentials=True) 

is the official document of Flask-CORS

http://flask-cors.readthedocs.io/en/latest/

now we look at the actual case of

 from flask_cors import CORS app = Flask to solve the problem (__name__, r'/*') # is a wildcard, let the server all URL are allowed to cross domain requests CORS (APP, resources=r'/*') # upload @uploadFiles.route ('/upload', methods=['POST']) def (uploadFile): result_text = {"statusCode": 200, "message": the file upload success "} (jsonify (r response = make_response Esult_text = response.headers['Access-Control-Allow-Origin'] * response.headers['Access-Control-Allow-Methods'])) ='OPTIONS, HEAD, GET, POST'response.headers['Access-Control-Allow-Headers'] return response

='x-requested-with' POSTMAN

due to the problems encountered in the project is a multi routing routing, routing through the main jump to the sub route beginning, allow cross domain configuration settings in the file upload sub routing, OPTIONS request response value does not return the expected header file, will set up

to

cross domain on

can be set in the main route This is the whole content of the sample code for Flask to solve the cross - domain problem, and I hope to help you. Interested friends can continue to refer to other relevant topics of the station, if there are shortcomings, welcome the message. Thank you for the support of our friends!

This paper fixed link:http://www.script-home.com/flask-to-solve-cross-domain-problem-sample-code.html | Script Home | +Copy Link

Article reprint please specify:Flask to solve cross domain problem sample code | Script Home

You may also be interested in these articles!