ajax - 在cakephp中使用ajax发表评论导致404错误,但本地没有错误?

标签 ajax forms cakephp http-status-code-404

使用为与 Jquery 一起使用而创建的 ajax 帮助程序,我创建了一个将评论发布到“/comments/add”的表单,这在我的本地 wamp 服务器上按预期工作,但在我的生产服务器上却不然。在我的生产服务器上,我收到“404 错误,找不到/comments/add”。

我花了相当多的时间寻找解决方案,但到目前为止还没有成功。我一直专注于试图找出差距,但我什么也没发现。

以下是一些观察结果:

  • 在本地 wamp 服务器上按预期工作
  • requestHandler 被列为组件
  • 本地和生产服务器上的文件相同
  • controllers 文件夹具有写入权限
  • autoLayout 和 autoRender 均设置为 false

这是我认为的表格:

     <div class="comments form">
     <?php echo $ajax->form('/comments/add', 'tournament', array('url' => '/comments/add', 'update' => 'Comments', 'indicator' => 'commentSaved'));?>
         <fieldset>
             <legend><?php __('Add Comment');?></legend>
             <div id="commentSaved" style="display: none; float: right;">
                <h2>Loading...</h2>
             </div>
         <?php
             echo $form->hidden('Comment.foreign_id', array('value' => $tournament['Tournament']['id']));
             echo $form->hidden('Comment.belongs_to', array('value' => 'Tournament'));
             echo $form->input('Comment.name');
             echo $form->input('Comment.email');
             echo $form->input('Comment.web', array('value' => 'http://'));
             echo $form->input('Comment.content');
         ?>
         </fieldset>

     <?php echo $form->end('Submit');?>
     </div>

这是我的“评论 Controller 添加操作”:

function add() {
   if($this->RequestHandler->isAjax())
   {
           $this->autoLayout = false;
           $this->autoRender=false;
       $this->Comment->recursive =-1;
       $commentInfos = $this->Comment->findAllByIp($_SERVER['REMOTE_ADDR']);
       $spam = FALSE;
       foreach($commentInfos as $commentInfo)
       {
     if ( time() - strtotime($commentInfo['Comment']['created']) < 180)
     {
         $spam = TRUE;
     }
       }

       if ($spam === FALSE)
       {

     if (!empty($this->data)) {
         $this->data['Comment']['ip'] = $_SERVER['REMOTE_ADDR'];
         $this->Comment->create();
         if ($this->Comment->save($this->data)) {

             $this->Comment->recursive =-1;
             $comments = $this->Comment->findAll(array('Comment.foreign_id' => $this->data['Comment']['foreign_id'], 'Comment.belongs_to' => $this->data['Comment']['belongs_to'], 'Comment.status' =>'approved'));

             $this->set(compact('comments'));
             $this->viewPath = 'elements'.DS.'posts';
             $this->render('comments');
         }
     }
       }
       else
       {
             $this->Comment->recursive =-1;
             $comments = $this->Comment->findAll(array('Comment.foreign_id' => $this->data['Comment']['foreign_id'], 'Comment.belongs_to' => $this->data['Comment']['belongs_to'], 'Comment.status' =>'approved'));
             $this->set(compact('comments'));
             $this->viewPath = 'elements'.DS.'posts';
             $this->render('spam');
       }
   }
   else
   {
       $this->Session->setFlash(__('Invalid Action. Please view a post to add a comment.', true));
       $this->redirect(array('controller' => 'pages', 'action'=>'display', 'home'));
   }
     }

如您所见,我已确保“autoLayout”和“autoRender”设置为 false,但在 Firebug 中我仍然收到 404 错误,指出在生产服务器上找不到/comments/add。

我还应该指出,我正在使用 jquery、jquery.form 和 jquery.editable 以及为与 jquery 一起使用而创建的 ajax 帮助程序。

非常感谢任何有关如何解决此问题的帮助甚至建议。

干杯, 保罗

最佳答案

事实证明,这是由最后几行代码引起的,调用了我不在注释中使用的 session 。

 else
 {
     $this->Session->setFlash(__('Invalid Action. Please view a post to add a comment.', true));
     $this->redirect(array('controller' => 'pages', 'action'=>'display', 'home'));
 }

吸取的教训,请小心从教程中获得的代码。它可能有一些你不使用的东西。

-保罗

关于ajax - 在cakephp中使用ajax发表评论导致404错误,但本地没有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2580529/

相关文章:

php - 显示错误的发布日期(wordpress)

jquery - 表单提交后重定向出现问题

javascript - 将表单数据发送到按钮提交的弹出窗口

mysql - 如何根据条件关联的模型字段对查询结果进行排序?

javascript - 通过 API 获取 tumblr 照片帖子内容,但对于照片集帖子的排列方式有所不同

ajax - Extjs 4 通过ajax调用下载文件

php - 尝试自定义 Flash 消息时出现错误 $content_for_layout

php - 使用 CakePHP 3.x 设置 ReactJS 的最佳方法是什么?

javascript - 加载到浏览器内存中的最大数据量

jquery - 如何抓取随机混合的输入和段落的内容