由于cors,离子应用程序无法连接到php应用程序

Below is my ionic code. An angular service making an API call.

 var login = function (user)
    {
      return $q(function(resolve, reject)
      {
        $http.post(API_ENDPOINT.url + '/login',user)
          .then(function (result)
          {
            if(result.data.success)
            {
              resolve(result.data.msg);
            }
            else
            {
              reject(result.data.msg);
            }
          });
      });
    };

This below is the code on the php end.

$app    =   new \Slim\Slim();
// Get request headers as associative array
$headers                = $app->request->headers;
$http_origin            = $headers['Origin'];
$allowed_http_origins   = array("http://localhost","http://localhost:80",
    "http://localhost:8080","http://localhost:63342","null",
    "http://localhost:8100","http://localhost:8888","http://localhost:8889");

if (in_array($http_origin, $allowed_http_origins)){  
    $app->response->headers->set('Access-Control-Allow-Origin',"$http_origin");
}

$app->response->headers->set('Access-Control-Allow-Methods','GET, PUT, POST, DELETE, OPTIONS');
$app->response->headers->set('Access-Control-Allow-Credentials','true');
$app->response->headers->set('Access-Control-Allow-Headers','*');
$app->response->headers->set('Access-Control-Allow-Origin',"$http_origin");


$app->post('/login/', 'Login' );

The app is having an issues with the CORS, it gives an error of Access-Control-Allow-Origins not present.

This is a problem when you test it from localhost this is not a problem when you build it to a device.

You could try THIS chrome addon to work around CROS.