Solve Cross Origin Resource Sharing with Flask

Solve Cross Origin Resource Sharing with Flask

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

Flask:XMLHttpRequest at '...' from origin has been blocked by CORS policy No 'Access-Control-Allow-Origin' header is present on the requested resource

i found a solution, the problem was not my python code it was my java code :/
a simple add of " transports: ["polling"] " solve the problem

import io from "socket.io-client";
let endPoint = "http://localhost:5000";
let socket = io.connect(`${endPoint}`, { transports: ["polling"] });

function receiveData(cb) {
socket.on("receivedData", (resp) => cb(resp));
}

export { receiveData };


Related Topics



Leave a reply



Submit