{"id":1260,"date":"2015-03-04T12:40:14","date_gmt":"2015-03-04T12:40:14","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1260"},"modified":"2015-08-10T06:49:33","modified_gmt":"2015-08-10T06:49:33","slug":"handling-ionic-cors-issue","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/quick-tips\/handling-ionic-cors-issue\/","title":{"rendered":"Handling Ionic CORS issue"},"content":{"rendered":"<p>CORS = Cross origin resource sharing &#8211; a pain in the butt when trying to do an ajax request when developing locally.<\/p>\n<p>For local testing\u00a0I ended up using a Chrome plugin:\u00a0<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/allow-control-allow-origi\/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en\">https:\/\/chrome.google.com\/webstore\/detail\/allow-control-allow-origi\/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en<\/a>.<\/p>\n<p>The site <a href=\"http:\/\/enable-cors.org\/\">http:\/\/enable-cors.org\/<\/a>\u00a0gives a lot of info on how to solve the cors issue if you control the API server. Namely, for Apache:\u00a0<a href=\"http:\/\/enable-cors.org\/server_apache.html\">http:\/\/enable-cors.org\/server_apache.html<\/a>.<\/p>\n<p>But, TBH, all this ended up not working for me, so what I did was I created a &#8220;proxy&#8221; .php page which is called first (not calling the json file directly) and the PHP code for that page looks like this:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\theader('Access-Control-Allow-Origin: *'); \r\n\techo file_get_contents(\"myJSON.json\");\r\n?&gt;<\/pre>\n<p><strong>edit:<\/strong> I answered a similar question on StackOverflow and I wrote a <a href=\"http:\/\/www.nikola-breznjak.com\/blog\/ionic\/posting-data-from-ionic-app-to-php-server\/\">full tutorial with downloadable project<\/a> from Github about this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CORS = Cross origin resource sharing &#8211; a pain in the butt when trying to do an ajax request when developing locally. For local testing\u00a0I ended up using&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,15],"tags":[],"class_list":["post-1260","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ionic","category-quick-tips"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1260"}],"version-history":[{"count":6,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1260\/revisions"}],"predecessor-version":[{"id":1923,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1260\/revisions\/1923"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1267"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}