问题描述
我试图在运行于localhost:8100
离子应用程序中使用$http.post()
将登录凭据发布到CakePHP API
。
我正进入(状态
XMLHttpRequest cannot load http://localhost/app/users/login.
No 'Access-Control-Allow-Origin'
header is present on the requested resource.
Origin 'http://localhost:8100' is therefore not allowed access.
然后我尝试使用$http.jsonp()
并且能够通过url发送我的登录凭据,例如
$http.get(
'http://localhost/app/users/login.json?
callback=JSON_CALLBACK
&username='+data.username+'
&password='+data.password
);
现在我可以使用以下方式登录Cake Controller
if ($this->request->is('get')) {
$this->request->data['User']['username']=$_GET['username'];
$this->request->data['User']['password']=$_GET['password'];
if ($this->Auth->login()) {
$user['name']=$_SESSION['Auth']['User']['name'];
$user['id']=$_SESSION['Auth']['User']['id'];
} else {
$user['name']='Fake';
$user['id']='Fake';
}
$user = json_encode($user);
$user=$this->finish($user);
$this->set(array('user'=>$user,'_serialize'=>array('user')));
}
一切对我来说都很好,但是我的问题是“ 实施登录是否是最佳实践??? ”。 请给我建议。 谢谢
1楼
user2889331
0
已采纳
2015-08-03 10:38:58
你可以尝试使用
$this->response->header('Access-Control-Allow-Origin', '*');
在控制器中启用cakephp中的CORS,并使用POST方法登录而不是JSONP。