Javascript Ionic和CakePHP登录
我试图在运行于Javascript Ionic和CakePHP登录,javascript,angularjs,ionic-framework,cakephp-2.0,ionic,Javascript,Angularjs,Ionic Framework,Cakephp 2.0,Ionic,我试图在运行于localhost:8100的ionic应用程序中使用$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 there
localhost:8100
的ionic应用程序中使用$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
);
现在我可以使用
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')));
}
对我来说一切都很好,
但我的问题是“实现登录是最佳实践吗?”。
请推荐我。
谢谢您可以尝试使用
$this->response->header('Access-Control-Allow-Origin', '*');
在控制器中,在cakephp中启用CORS,并使用POST方法登录而不是JSONP。您可以尝试使用
$this->response->header('Access-Control-Allow-Origin', '*');
在controller中启用cakephp中的CORS,并使用POST方法登录而不是JSONP。经过大量学习,现在通过在cakephp控制器中设置头来消除CORS错误,并找到了一些关于AngularJS中登录身份验证的最佳参考。谢谢!:)通过在cakephp控制器中设置头部,我们已经了解了很多cors错误,并且在AngularJS中找到了一些登录验证的最佳参考。谢谢!:)