Ajax request won't work


#1

If I replace in my controller function $this->request->is[‘ajax’] with $this->request->is[‘get’] and I send
/pricegroups/reorder?Pricegroup[0]=1&Pricegroup[1]=2&Pricegroup[2]=3&Pricegroup[3]=4
the controller function reorder works fine.

public function reorder() {		
	if ($this->request->is(['ajax'])) {
		
		foreach ($this->request->query['Pricegroup'] as $key => $value) {
			$pricegroup = $this->Pricegroups->get($value, [
			            'contain' => []
			        ]);
			$pricegroup->psort = $key;
			$this->Pricegroups->save($pricegroup);
		}
		exit();
	}
}

The js script in the Template/Pricegroups/index.ctp give me a right sortable list like this

Pricegroup[]=3&Pricegroup[]=4&Pricegroup[]=2&Pricegroup[]=1

$(document).ready(function () {
		$("#my-list").sortable({
			stop:function (event, ui) {
				$.post("http://localhost/myapp/pricegroups/reorder", $("#my-list").sortable("serialize"))
			}
		});
	});

but nothing will work in my controller. Where is my error?


#2

Now it works…

Here the controller action

public function reorder() {		
	if ($this->request->is(['ajax'])) {
		foreach ($this->request->data['Pricegroup'] as $key => $value) {
			$pricegroup = $this->Pricegroups->get($value, [
			            'contain' => []
			        ]);
			$pricegroup->psort = $key;
			$this->Pricegroups->save($pricegroup);
		}
		exit();
	}
}

and here the js script.

$(document).ready(function () {
		$("#my-list").sortable({
			stop:function (event, ui) {
				var request = $.post("http://localhost/myapp/pricegroups/reorder", $("#my-list").sortable("serialize"));
			
			request.done(function( msg ) {
			  $( "#log" ).html( msg );
			});

			request.fail(function( jqXHR, textStatus ) {
			  alert( "Request failed: " + textStatus + " " + $("#my-list").sortable("serialize"));
			});
		}
	});
});

The list

<ul id="my-list"> 
		<?php $count=0; ?>
        <?php foreach($pricegroups as $pricegroup): ?>                  
    	<?php $count ++;?> 
		<?php if($count % 2): echo '<li '; else: echo '<li class="zebra"'; ?> 
		<?php endif; ?>       
        <?php echo "id='Pricegroup_" . $pricegroup->id . "'>" ?><div style="float:left;"><?php echo $pricegroup->name; ?></div></li>
        <?php endforeach; ?>
        <?php unset($pricegroup); ?>
</ul>