【问题标题】:Forbidden Error In JavaScript Ajax Call in CakePHP 3CakePHP 3 中 JavaScript Ajax 调用中的禁止错误
【发布时间】:2017-03-23 18:03:33
【问题描述】:

我看到了类似的问题,但我无法找到解决方案。请帮忙处理这段代码,我是 javascript 新手

我的 JavaScript 代码:

function checkId(id_corpus){

  var dataSet = {identity_number: id_corpus};
  var requestUrl = appBaseUrl+'users/check-id-presence';
  alert(id_corpus);
   $.ajax({
      type: "POST",
      url: requestUrl,
      data: dataSet,
      success: function(result) {
          if(result == false){
              $('#ino').css('background-color', 'red');
              $('#ino').css('color', 'black');
            }

      },
      error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus, errorThrown);
      }
  });
  }

上面的 JS 代码被下面的 CakePHP 视图调用:

$this->Form->input('identity_number', ['style' => 'background-color : black; color : #1798A5;', 'id' => 'ino', 'onkeypress' => 'checkId(this.val)']);

以下是控制器代码

public function checkIdPresence()
     {
       $this->autoRender = false;
       $id_corpus = $this->request->data['identity_number'];
       $check = $this->Users->find()->where(['identity_number LIKE' => '%'.$id_corpus.'%']);
       if((iterator_count($check)) > 0){
         echo false; //Corpus Exists

     }else{

       echo true;
   }
}

我卡在“禁止错误”中,我想提醒您,我正在使用类似的 AJAX 进行图像显示(如下所示),它没有显示任何错误:

    function fetch(user_id, photo, photo_dir)
{


  var dataSet = {id: user_id};
  var requestUrl = appBaseUrl+'users/admin-side-nav-details';
  var imageUrl = 'http://localhost/media/images/users/photo/'+photo_dir+'/'+'100x100_'+photo;
   $.ajax({
      type: "POST",
      url: requestUrl,
      data: dataSet,
      success: function(result) {
              $('#display_info').html(result);
              var image = "<img src ="+imageUrl+" />"
              console.log(image);
              $('#display_image').html(image);

      },
      error: function(jqXHR, textStatus, errorThrown) {
          console.log(textStatus, errorThrown);
      }
  });
}

编辑:我的 Auth 组件设置

public function initialize()
  {
  $this->loadComponent('Auth', [
          'authenticate' => [
            'Form' => [
              'fields' => ['username' => 'email', 'password' => 'password']
            ]
          ],
          'loginAction' => [
              'controller' => 'Users',
              'action' => 'login',
          ],
          'authError' => 'Are you sure, you want to enter?',
          'logoutAction' => [
            'controller' => 'Users',
            'action' => 'login',
          ],
        ]);

【问题讨论】:

  • 什么是“禁止错误”?目前尚不清楚该错误来自何处。最好的猜测与您的身份验证/授权设置有关。
  • 控制台只是显示“禁止错误”,没有别的。
  • 如果您检查这些请求的服务器日志,它们是否显示为 403 响应代码?
  • 这是 403 错误。完成

标签: javascript php jquery ajax cakephp-3.0


【解决方案1】:

检查https://book.cakephp.org/3.0/en/controllers/components/authentication.html#handling-unauthenticated-requests。 “如果身份验证器返回 null,AuthComponent 会将用户重定向到登录操作。如果它是 AJAX 请求并且指定了配置 ajaxLogin,则呈现该元素,否则返回 403 HTTP 状态代码。”

在我看来,您似乎没有指定任何授权方案,因此根据https://book.cakephp.org/3.0/en/controllers/components/authentication.html#using-no-authorization“如果您不使用授权方案,请确保自己在控制器的 beforeFilter 或其他组件中检查授权。”

您可以使用以下方法公开操作(在 beforeFilter 或 initialize 中):

// Allow all actions
$this->Auth->allow();

// Allow only the index action.
$this->Auth->allow('index');

// Allow only the view and index actions.
$this->Auth->allow(['view', 'index']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 2011-09-04
    • 2012-10-07
    • 1970-01-01
    相关资源
    最近更新 更多