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

时间:2010-04-05 19:14:17

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

使用为Jquery创建的ajax帮助器我创建了一个将注释发布到“/ comments / add”的表单,这在我的本地wamp服务器上按预期工作,但在我的生产服务器上没有。在我的生产服务器上,我收到'404错误,无法找到/评论/添加'。

到目前为止,我花了很多时间寻找一个没有运气的决议。我专注于试图找出差距,但没有任何事情发生在我身上。

以下是一些观察结果:

  • 在本地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>

这是我的'评论的控制器添加动作:

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帮助器。

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

干杯, 保罗

2 个答案:

答案 0 :(得分:2)

原来这是由最后几行代码引起的,调用了我在评论中不使用的会话。

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

经验教训,请注意从教程中获得的代码。它可能有你不使用的东西。

-Paul

答案 1 :(得分:1)

您是否确保生产服务器上存在.htaccess文件?如果是这样,是否安装了mod_rewrite,并且主目录中是否允许.htaccess覆盖?似乎问题必然在于您的本地环境和生产环境之间的某些差异,而不是代码本身。

相关问题