使用为与 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/