Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask CORS not functioning
#2
Hello,

I am attempting to use Flask, but I am unable to configure CORS to allow for requests from other ports.

My current Python server:

from flask import Flask, request
from urllib.request import urlopen
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=['POST'])
@cross_origin()
def convert():
    print("Received Request")
    print(request.json())
    return "Success"


if __name__ == "__main__":
    app.run()
Despite this, any requests made to this localhost, from another port, with the following headers:

"method": "POST",
            "Access-Control-Allow-Origin": "*",
            "headers": {
               "Content-Type": "application/json"
            },
Results in the following error:

Error:
Access to fetch at 'http://localhost:5000/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Reply


Messages In This Thread
Flask CORS not functioning - by michaelnicol - Jul-02-2023, 05:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Allowing CORS in Django app with a React frontend Dexty 0 3,259 Dec-22-2021, 11:38 PM
Last Post: Dexty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020