CORS is the way to share resources between different domains. while avoiding potential cross site scripting attacks. If you do not understand what CORS is or just want to learn more on it, I suggest you read one of the following or all if you're awesome:
- HTTP access control (CORS)
- Using CORS
- W3C Cross-Origin Resource Sharing
- Wikipedia- Cross-origin resource sharing
An understanding of this will definitely help you in future projets.
-
Make virtualenv environment
-
With virtualenv-wrapper
$> mkdir cors-test && cd cors-test $> mkvirtualenv cors-test
-
With virtualenv
$> mkdir cors-test && cd cors-test $> virtualenv env && source env/bin/activate
-
-
Install requirements
$> pip install -r requirements.txt
-
To execute the Flask app just run
$> python app.py
This will use Flask's default port 5000
-
You can use Python's development server to serve your html
$> python-m SimpleHTTPServer 8000
The port doesn't need to be 8000 just a high value port that isn't in use.
-
Open your browser to
http://localhost:5000/
. You should get{'hello': 'world'}
in your browser's JS console. -
Now that that's working open
http://localhost:8000/give_me_data
in your browser. Enter any value in the input text field and click the button below it. You should see{'name': <your_input_val>}
in the browser's JS console and the terminal where the Flask app is running.
This simple example saves me tons of time. The only notice: since
Flask-Cors
andFlask-RESTful
updated it should be imported like: