Handling Ionic CORS issue
CORS = Cross origin resource sharing – a pain in the butt when trying to do an ajax request when developing locally.
For local testing I ended up using a Chrome plugin: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en.
The site http://enable-cors.org/ gives a lot of info on how to solve the cors issue if you control the API server. Namely, for Apache: http://enable-cors.org/server_apache.html.
But, TBH, all this ended up not working for me, so what I did was I created a “proxy” .php page which is called first (not calling the json file directly) and the PHP code for that page looks like this:
<?php header('Access-Control-Allow-Origin: *'); echo file_get_contents("myJSON.json"); ?>
edit: I answered a similar question on StackOverflow and I wrote a full tutorial with downloadable project from Github about this.
Leave a Comment