Ajax 302 Errors in checkbox toggle script

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.

302 is a redirect, could be that you have same redirections there (mod_rewrite?)…

The only redirects I have are the htaccess defaults from Yii, including the one to eliminate the index.php file.




Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on


# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


# otherwise forward it to index.php

RewriteRule . index.php



The strange thing is that I have the same AJAX checkbox toggle working fine somewhere else in the application with no errors.

That ajax requests a certain URL… try that URL manually from the browser… see if then it makes the redirect… check the code behind that URL controller/action

I tried running the AJAX URL on its on, and there was no sign of a redirect that I could determine. The URL remained the same, and the result was a blank page displaying the ‘1’ or ‘0’ it is designed to return.

The controller code does a simple model lookup, and based on that determines whether to delete a row and return ‘0’ or save a row and return ‘1’. Validation is set to “false” for the save. There’s not place in there that it’s sending any HTML headers.

Am I missing something? Where else do you think I could look?

As this is all “clean” I suppose it’s something in the JS code… do you have some “special” ajax handlig function?

No. Not that I know of. But I think I found the source of the problem. I have a local version of the app (running in MAMP on my MacBook) and a live version (running on a linux server over in Germany). I ran into the problem when we had an Internet outage, and I needed to work locally. Seems it has something to do with MAMP or VirtualHostX (the software I use to manage my virtual host) producing a redirect.

Sorry to drag you on a wild goose chase. Turns out everything is good with the code.

Thanks very much for your help.

wow… glad you found out the problem… I was out of “creative” ideas :D

Yes, me too. That’s what I meant by “maddening” — since from the coding perspective — everything was as it should be.