AJAX Request CORS Policy error with Flask
You can use this to test that it's actually a CORS issue. This worked for me.
@app.after_requestdef after_request(response): header = response.headers header['Access-Control-Allow-Origin'] = '*' header['Access-Control-Allow-Headers'] = 'Content-Type, Authorization' header['Access-Control-Allow-Methods'] = 'OPTIONS, HEAD, GET, POST, DELETE, PUT' return response
The header Access-Control-Allow-Origin
must be returned from Flask, not requested by AJAX. So it would be something like this:
def get_content(): return "It works!", 200@app.route('/')def home(): content, status_code = get_content() headers = {'Access-Control-Allow-Origin': '*'} return content, status_code, headers
Thank you all for the help. I already solved the problem. I was making requests to HTTP PTZ Camera which don't let me make requests from client side (I've no idea why). So I hack the problem, and now I'm making requests to my server, and my server make the request to the camera. Here you've what I'm doing:
$.ajax({url: 'http://127.0.0.1:5000/teste00', type: "GET", success: function (result) {}, });
On server side:
@system.route('/teste00')def teste(): response = requests.get('camera_url') return response.content