Tag Archives: call

Cakephp3: sending json response message to view

23666Method1:

$this->response->body(json_encode($msg));
return $this->response;

Method2:

$this->set('msg', $msg);
$this->set('_serialize', ['msg']);

//controller action

public function test(){		
		
		$msg="this is a call";
		
                //method 1 (chose a method)
		$this->response->body(json_encode($msg));
		return $this->response;	
		
		//method 2
		$this->set('msg', $msg);
		$this->set('_serialize', ['msg']);		
		
}

//javascript function call in view
function delete_all(){
	jQuery.ajax({
			type:'POST',
			async: true,
			cache: false,
			url:'mycontroller/test.json',
			success: function(response){					
				//success
				console.log(response);  				
			},
			error: function(response){					
				console.log(response);
			}
		});
}