# AJAX in cake 3.6 - Error: Controller actions can only return Cake\\Http\\Response or null

**URL:** <https://discourse.cakephp.org/t/ajax-in-cake-3-6-error-controller-actions-can-only-return-cake-http-response-or-null/4994>\
**Category:** Need Help\
**Created:** [September 25, 2018, 7:26am UTC](https://discourse.cakephp.org/t/ajax-in-cake-3-6-error-controller-actions-can-only-return-cake-http-response-or-null/4994 "2018-09-25T07:26:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KunalSingh](https://avatars.discourse-cdn.com/v4/letter/k/3be4f8/32.png) [@KunalSingh](https://discourse.cakephp.org/u/KunalSingh)\
**Post date:** [September 25, 2018, 7:26am UTC](https://discourse.cakephp.org/t/ajax-in-cake-3-6-error-controller-actions-can-only-return-cake-http-response-or-null/4994/1 "2018-09-25T07:26:42Z")

</div>

I have some problem using ajax, please help me .

my controller action is :-

public function getBlocksByDistrict(){

```
	$this -> autoRender = false;  
	
	$arr = array();
	if($this->request->is('ajax')){
		$id = $this->request->getQuery('id'); //echo $id;die;
		$this->loadModel('BlockCity');
		
		$blockCityData = TableRegistry::getTableLocator()->get('BlockCities')->find('all', array('fields'=>array('BlockCities.rgi_block_code','BlockCities.name'), 'conditions'=>array('BlockCities.rgi_district_code='."'".$id."'"), 'recursive'=>-1));
		$obj_to_array_blockCityData = $blockCityData->toArray();
		
		$blocks = array();
		foreach($obj_to_array_blockCityData as $key => $value){
			$blocks[$value['rgi_block_code']] = $value['name'];
		}
		//echo "<pre>";print_r(json_encode($blocks));die;
		
		return json_encode($blocks);
		//$this->set(compact('blocks'));
        //$this->set('_serialize', ['blocks']);
	}
}

```

and JS function in ctp file :-

(document).ready(function(){ (’#TransactionRgiDistrictCode’).change(function(){  
var url = (this).attr('rel'); //alert(url);return false; var dist\_id = (’#TransactionRgiDistrictCode’).val();

```
	$.ajax({
		type: 'GET',
		url: url,
		data: ({id : dist_id}),
		beforeSend: function(xhr) 
		{
			xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		},
		success: function(response) 
		{	
			if (response) 
			{ 
				//alert(response);
				console.log("content = " + response);
				return false;
				var result = JSON.parse(response); 
				$.each(result, function(val, text) {
					$('#TransactionRgiBlockCode').append(
						$('<option></option>').val(val).html(text)
					);  
				});	
			}
		},
		error: function(e) 
		{
			alert("An error occurred: " + e.responseText.message);
			console.log(e);
		}
	});
});

```

});

I have followed the below url with the sandbox url as well.  
[http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/](http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/)

please help me where I am mistaking.

I will be really very thankful for this. Please help will be appreciated.

---

<div class="post-metadata">

**Author:** ![christ57](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@christ57](https://discourse.cakephp.org/u/christ57)\
**Post date:** [September 25, 2018, 3:52pm UTC](https://discourse.cakephp.org/t/ajax-in-cake-3-6-error-controller-actions-can-only-return-cake-http-response-or-null/4994/2 "2018-09-25T15:52:14Z")

</div>

Hello,

Try to return your response in your controller like this :

$this-\>response-\>body(json\_encode($blocks));

return $this-\>response;

---

<div class="post-metadata">

**Author:** ![KunalSingh](https://avatars.discourse-cdn.com/v4/letter/k/3be4f8/32.png) [@KunalSingh](https://discourse.cakephp.org/u/KunalSingh)\
**Post date:** [October 8, 2018, 10:56am UTC](https://discourse.cakephp.org/t/ajax-in-cake-3-6-error-controller-actions-can-only-return-cake-http-response-or-null/4994/3 "2018-10-08T10:56:47Z")

</div>

> [@christ57](#):
>
> return $this-\>response;

Hello christ57,

Thanks for your reply. It works. Thanks a lot.
