Created
December 3, 2019 14:06
-
-
Save tvorogme/442b976af76189f4b2092f8d59638849 to your computer and use it in GitHub Desktop.
Fix cors request to flask from javascript fetch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
def cors(f): | |
def fix_cors(*args, **kwargs): | |
if request.method == 'OPTIONS': | |
response = jsonify('') | |
response.headers.add('Access-Control-Allow-Origin',request.headers['Origin'] if 'Origin' in request.headers else '*') | |
response.headers.add('Access-Control-Allow-Headers', request.headers['Access-Control-Request-Headers'] if 'Access-Control-Request-Headers' in request.headers else '') | |
response.headers.add('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') | |
response.headers.add('Access-Control-Max-Age', '86400') | |
else: | |
return f(*args, **kwargs) | |
return fix_cors | |
@app.route('/', methods=['GET', 'OPTIONS']) | |
@cors | |
def profession_api(): | |
... | |
response = jsonify([]) | |
response.headers.add('Access-Control-Allow-Origin',request.headers['Origin'] if 'Origin' in request.headers else '*') | |
... | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment