Send Form data with ajax to cakephp controller

23666The following example shows how to submit a form with AJAX to a Cakephp controller;

Jquery Javascript Funtion;

	/*
	sends the form to the controller, ensure form fields names match up 
	with escpected values.
	*/
	function testajaxaddv3(){		
		jQuery.ajax({
			type:'POST',
			async: true,
			cache: false,
			url: 'http://localhost/mycakeapp/messages/ajaxadd',
			success: function(response) {					
				//success
				console.log(response);                
			},
			error: function(response) {					
				console.log(response);
			},
			data:jQuery('form').serialize()
		});
	}

CakePHP 3 Form:

<?= $this->Form->create(null) ?>
<fieldset>
<legend><?= __('Add Quizzes Answer') ?></legend>
<?php
echo $this->Form->input('message');
?>
</fieldset>

<?= $this->Form->end() ?>
</div>

<?php
echo $this->Form->button('save',['onclick'=>'testajaxaddv3()']);
?>