Search code examples
ionic-frameworkionic2

ionic problem No 'Access-Control-Allow-Origin'


I'm working on an ionic apps. My problem is: when I try to get data from server I got this:

XMLHttpRequest cannot load https://mywebsite.com/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

I already try to add this to .htaccess:

<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>

And this to my api page (PHP): header('Access-Control-Allow-Origin: *');

but still not working

$http.get(url).success(function(response) {...}


Solution

  • Put it on top of your PHP file like:

    <?php
    header("Access-Control-Allow-Origin: *");
    // then your stuff goes here
    
    ?>
    

    Note: as with all uses of the PHP header function, this must be before any output has been sent from the server.