CakePHP AuthComponent使用Ajax登录而不使用表单?
我想使用CakePHP 2.2创建一个应用程序,允许非浏览器客户端连接和存储/访问数据。我一直在看用户在UserController中使用以下代码片段登录的示例:CakePHP AuthComponent使用Ajax登录而不使用表单?,ajax,json,cakephp,authentication,Ajax,Json,Cakephp,Authentication,我想使用CakePHP 2.2创建一个应用程序,允许非浏览器客户端连接和存储/访问数据。我一直在看用户在UserController中使用以下代码片段登录的示例: public function login() { if($this->request->is('post')) { if($this->Auth->login()) { $this->Session->setFlash('Lo
public function login()
{
if($this->request->is('post'))
{
if($this->Auth->login())
{
$this->Session->setFlash('Login Passed');
}
else
{
$this->Session->setFlash('Login Failed');
}
}
}
这是通过浏览器在浏览器上显示表单完成的,用户填写“用户名”和“密码”,然后单击按钮提交。我知道,使用Javascript和Ajax,您可以“序列化”表单并将其发送到服务器,但假设我不想(或不能)使用表单,而只是让客户端发送两位信息:“用户名”和“密码”,我如何通过上面的“登录”方法处理这些数据?我知道Auth->login
接受一个可选参数$user
,但是有没有办法从用户名/密码组合中获取$user
,这样我就可以将它传递给Auth->login
?我想象这样的情况:
public function login()
{
if($this->request->is('post'))
{
if($this->Auth->login())
{
$this->Session->setFlash('Login Passed');
}
else
{
$this->Session->setFlash('Login Failed');
}
}
else if ($this->RequestHandler->isAjax())
{
$tmpUser = getUser ('username', 'password'); // ????? ===> Whatever call is needed here.
if($this->Auth->login($tmpUser))
{
$this->Session->setFlash('Login Passed');
}
else
{
$this->Session->setFlash('Login Failed');
}
}
}
您可以在使用Ajax登录实现Auth时使用该属性。
除此之外,您还可以尝试以下代码:
public function login()
{
if($this->request->is('post'))
{
if($this->Auth->login())
{
$this->Session->setFlash('Login Passed');
}
else
{
$this->Session->setFlash('Login Failed');
}
}
else if ($this->RequestHandler->isAjax())
{
$tmpUser['User']['username'] = $this->request->params['username'];
$tmpUser['User']['password'] = $this->request->params['password'];
if($this->Auth->login($tmpUser))
{
$this->Session->setFlash('Login Passed');
}
else
{
$this->Session->setFlash('Login Failed');
}
}
}
您可以使用firebug控制台检查响应。您找到了最好的方法吗?登录后设置的会话id如何?