Skip to content
Advertisement

Solve Cross Origin Resource Sharing with Flask

For the following ajax post request for Flask (how can I use data posted from ajax in flask?):

JavaScript

I get a Cross Origin Resource Sharing (CORS) error:

JavaScript

I tried solving it in the two following ways, but none seems to work.

  1. Using Flask-CORS

This is a Flask extension for handling CORS that should make cross-origin AJAX possible.

My pythonServer.py using this solution:

JavaScript
  1. Using specific Flask Decorator

This is an official Flask code snippet defining a decorator that should allow CORS on the functions it decorates.

My pythonServer.py using this solution:

JavaScript

Can you please give some some indication of why that is?

Advertisement

Answer

It worked like a champ, after bit modification to your code

JavaScript

I replaced * by localhost. Since as I read in many blogs and posts, you should allow access for specific domain

Advertisement