I’m getting mysterious (and maddening) 302 Found errors in a simple AJAX-powered checkbox toggle. Any Ajax pros out there who can help me figure out what’s wrong?
In the Controller:
public function actionConditionAjaxCheckboxToggle()
{
$model = Application_AcceptanceCondition::model()->findByAttributes(array('ap_application_id'=>$_REQUEST['ap_application_id'],'ap_acceptanceCondition_id'=>$_REQUEST['ap_acceptanceCondition_id']));
if (!is_null($model)) {
if ( $model->delete() )
echo json_encode (0);
} else {
$model = new Application_AcceptanceCondition;
$model->ap_application_id = $_REQUEST['ap_application_id'];
$model->ap_acceptanceCondition_id = $_REQUEST['ap_acceptanceCondition_id'];
if ( $model->save( false ) )
echo json_encode(1);
else
echo json_encode (0);
}
return 1;
}
In the View file:
<?php foreach($model->program->programType->acceptanceConditions as $condition) {
$checked = isset($model->acceptanceConditionList[$condition->id]);
$fieldName = 'acceptanceConditions_'.$condition->id; ?>
<tr>
<td class="left"><?php echo CHtml::checkBox($fieldName, $checked, array(
'ajax' => array(
'type'=>'POST', //request type
'url'=> $this->createUrl('admin/application/conditionAjaxCheckboxToggle'),
'dataType'=>'json',
'data' => array('ap_application_id' => $model->id,'ap_acceptanceCondition_id' => $condition->id),
'success' => 'js:function(val,textStatus,xhr){
alert(val);
if (val == 0) { document.getElementById("'.$fieldName.'").checked = false; }
else if (val == 1 ) { document.getElementById("'.$fieldName.'").checked = true; }
else alert("Server disconnect. Please reload the page and try again.");
}',
'error'=>'function (xhr, ajaxOptions, thrownError){
alert(xhr.status+" "+xhr.statusText+" "+thrownError);
}',
),
'id' => $fieldName,
));
?></td><td class="right"><label for="<?=$fieldName?>"><span class="assignee"><?=Yii::t('app',$condition->name)?></span><br /><?=Yii::t('app',$condition->acceptance_statement)?></label></td></tr>
<?php } ?>
According to FireBug, I’m getting the correct responses (1 or 0 for toggling). The controller action is successfully saving and deleting from the database. But the ajax code above keeps throwing a 302 Found error (!). I’m at a total loss as to why this is happening.
Much gratitude to anyone able to take a look.