using ajax to post comments in cakephp results in 404 error, but no errors locally?
Using an ajax helper created for use with Jquery I've created a form that posts comments to "/comments/add", and this works as expected on my local wamp server but not on my production server. On my production server I get a '404 error, cannot find /comments/add'.
I've spent quite a bit of time searching for a resolution with no luck so far. I've focused on trying to identify a gap but nothing jumps out at me.
Here are some observations:
- works as expected on local wamp server
- requestHandler is listed as component
- files on both local and production server are the same
- controllers folder has write access
- autoLayout and autoRender are both set to false
Here is the form in my view:
<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>
And here is my 'comment's controller add action:
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'));
}
}
As you can see I've made sure that 'autoLayout' and 'autoRender' are set to false, but in Firebug I still get a 404 error stating /comments/add cannot be found on the production server.
I should also point out that I'm using jquery, jquery.form and jquery.editable as well as a ajax helper created to be used with jquery.
Any help or even suggestions about how to trouble shoot this is really appreciated.
Cheers, Paul
Turns out that this was caused by the last few lines of code, calling a session that I don't use in comments.
else
{
$this->Session->setFlash(__('Invalid Action. Please view a post to add a comment.', true));
$this->redirect(array('controller' => 'pages', 'action'=>'display', 'home'));
}
Lesson learned, be careful with code you get from a tutorial. It might have something you don't use.
-Paul
Have you made sure your .htaccess files are present on the production server? If so, is mod_rewrite installed, and is .htaccess overriding allowed in the home directory? It seems like the problem must lie in some difference between your local environment and your production environment, and not in the code itself.
精彩评论