I'm using AJAX to check whether a username is in the database or not. Here is the controller code:
public function check_username(){
if($this->request->is('json')){
$user = $this->User->find('count', array('conditions' => array('username' => $this->request->data['username'])));
if($user === 0)
echo '1';
else
echo false;
}
$this->autoRender = false;
}
And I used AJAX on the view. That was ok and now I'm trying to use Json and changed my controller code to:
public function check_username(){
if($this->request->is('json')){
$user = $this->User->find('count', array('conditions' => array('username' => $this->request->data['username'])));
echo json_encode($user);
}
$this->autoRender = false;
}
This is my view code. This isn't working (no alert is being appeared):
var un = $('#username').val();
$.getJSON(
'/oes/users/check_username',
{username:un},
function(result){
alert(result);
});
Now anybody please help me make it working. I'm also new to Json. Thanks in advance.
public function check_username(){
if($this->request->is('json')){
$user = $this->User->find('count', array('conditions' => array('username' => $this->request->data['username'])));
echo json_encode($user);
exit;
}
$this->autoRender = false;
}
if it works , that the problem comes from loading view , (due to autoRender=false);