-2

I am trying to hit URL from browser through Ionic application and getting a CORS issue , the error is:

XMLHttpRequest cannot load http://school_mit.schnotify.com/api/v1/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.2.10:8100' is therefore not allowed access.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335

3 Answers3

0

You need to add http://192.168.2.10 as an allowed origin in your backend. Or you could use a proxy.

raj
  • 5,989
  • 7
  • 30
  • 62
0

CORS has to be enabled in the server side. Ionic applications work on Rest API. If you have a PHP back end you can use a framework like Slim and add these headers in your php file.

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization");
header('Access-Control-Allow-Headers: Content-Type, x-xsrf-token');
header("Access-Control-Allow-Headers: X-Requested-With");
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
        exit(0);
}

/*Resr of server file*/

?>

These headers in the server side should solve most of the CORS problem. You can implement the same in other languages

Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
-1

I had the same problem, I just fixed using this Chrome Plugin It allows adds Allow-Control-Allow-Origin: * to the header so you can make Ajax request from any source.

In Chrome you can also use flags

chrome --disable-web-security

or

--allow-file-access-from-files --allow-file-

But I prefer to use the extension because you can turn it on just when you needed without changing your server settings.

distante
  • 6,438
  • 6
  • 48
  • 90
  • Please always explain your suggested fix. Why does this plugin work and what does it do? – WJM Aug 07 '17 at 11:38