You can write your ajax code at bottom in view eg: templates/Folder-Name/YOUR-FILE.php
Or you can include a js file and write code there (In this case you should create js file in webroot folder)
In templates folder: ajax code will be look like :
var path="<?php echo $this->Url->webroot ?>/Controller-Name/Method-Name";
$.ajax({
type:"POST",
url:path,
data:{fname:'John',lname:'mac'},
success:function(result){
// on sucess whatever you want to do.
}
});
In controller you will have to define your method eg:
function MethodName(){
$fname= $this->request->getData('fname');
$lname= $this->request->getData('lname');
.....
}
I hope this will work for you.