I am creating a web application that uses CakePHP as the back end and JQuery as the front end. In order to communicate with the server, I wanted to make GET and Post requests from JQuery using ajax. Here is what I have right now:
function submit_registration(fname, lname, email, pass)
{
var arr = { data: {User: { fname: fname, lname: lname, email: email, password: pass }}};
$.post('http://localhost/cake2/users/add', JSON.stringify(arr), function(response){
alert(response);
})
}
That is in JQuery. Here is the add action in CakePHP:
public function add()
{
$this->layout = null ;
if ($this->request->is('post'))
{
$data = $this->request->input('json_decode');
$this->User->create();
if ($this->User->save($data))
{
$this->set('data', "success");
$this->render('/General/JsonMode2/');
}
else
{
$this->set('data', "failure to save");
$this->render('/General/JsonMode2/');
}
}
else
{
$this->set('data', "failure to post");
$this->render('/General/JsonMode2/');
}
}
It renders as JSON back to the front end. My problem is, nothing happens when I submit the request. I put alerts to ensure that the submit_registration function was being called. Can anyone give me a clue as the what is going on?