以下是我的离子代码。一个角度服务进行API调用。
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);
}
});
});
};
以下是php端的代码。
$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' );
该应用程序遇到CORS问题,它出现Access-Control-Allow-Origins错误。