1

通常,进行 ajax 调用相当简单。您只需解锁控制器中的操作并在您使用 ajax 的函数中指定:

public function beforeFilter(Event $event) {
    parent::beforeFilter($event);
    $this->Auth->allow(['checkUsername']);
    $this->Security->unlockedActions =['checkUsername'];
}

public function checkUsername() {
    $this->request->allowMethod(['ajax']);  
    // ...
}

不幸的是,在 CakePHP 3 中,这给了我下面的黑洞错误作为响应:

code: 400
message: "The request has been black-holed"
url: "/users/checkUsername.json"

当它是 /users/checkUsername(而不是 .json,这是其他地方推荐的解决方案)时,它的工作方式也相同。有任何想法吗?

4

1 回答 1

1

在 CakePHP 3.0 中它应该是:

public function beforeFilter(Event $event) {
  parent::beforeFilter($event);
  $this->Auth->allow(['checkUsername']);
  $this->Security->config('unlockedActions', ['checkUsername']);
}
于 2014-09-03T19:13:14.610 回答