JSONp support on flask apps
###The problem I was working on my sample project (Yeoman PetStore) and decided to call another service I had build a while back. This is when I started getting the following error:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
A quick search brought up the following:
When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Origin response header to tell the browser that the content of this page is accessible to certain origins. (An origin is a domain, plus a scheme and port number.) By default, Site B’s pages are not accessible to any other origin; using the Access-Control-Allow-Origin header opens a door for cross-origin access by specific requesting origins.
###My Solution
The correct fix is to change the server code to return the correct Access-Control-Allow-Origin
property in the header. However I wanted to take a quick route out and another way of fetching data from the server was to use JSONP.
I implemented my original service in python using flask. So I was hoping to easily extend the api to have jsonp support. Here an easy way of doing it:
NOTE: The following is not my code, but i can’t find a reference to the original author’s post
And now we can just decorate all our API calls with @support_jsonp
for jsonp support.
regards
======================================================================