cakephp 2.6.1 ajax 登录不工作

cakephp 2.6.1 ajax login not working

使用 ajax 登录时遇到问题。谁能指导我阅读一些有意义的文档。这听起来令人担忧。

In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.

$data['User']['email'] = "this";
$data['User']['password'] = "that";

$data = $this->request->input('json_decode', true);

$this->autoRender = false;
$this->response->type('json');

if ($this->Auth->login($data)){
    echo "access";
} else {
    echo "access denied";
}

它总是打印 "access".

在AppController.php

'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'posts',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email'),
                    'passwordHasher' => 'Blowfish'
                ),
            )
        ),

改用AuthComponent::identify()。但这可以(而且显然应该)做得更好。您的代码告诉我您对 CakePHP 中的 json 没有太多经验。

勾选this page of the manual. Also your request should be made with "Accept: application/json" then your data should automatically end up in $this->request->data and then login() should pick it up automatically。正确的方法是发送 Accept header 而不仅仅是依赖扩展,这是通用的而不是特定于 CakePHP 的。