Accessing APIs from Cordova app

I was trying to access an API whch was developed by myself in Python Flask from one of my Cordova app. But it was not working and I was not getting any error messages either. I know I should have used the Chrome browser remote testing for such debugging but that time I was not doing that. However, I developed a dummy html page to access my Flask API app and then I found that it was not working because No 'Access-Control-Allow-Origin' header is present on the requested resource error, that classic error of using ajax calls to access API.

To solve this problem I installed the CORS python module for Flask:

$ pip install flask_cors

And then add the following code in my Flask apps script:

from flask_cors import CORS, cross_origin

app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

And it worked 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top