I am developing functionality in Magento 2.3
version and calling controller function by using Ajax.
Controller function return product data and everything is working fine when customer is not login.
While when we try to login and return product data then it throw the error
"Exception: Warning: Cannot modify header information - headers already sent by (output started at app/code/Magento2/CustomProduct/Controller/Index/Index.php:74) in vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 148 in vendor/magento/framework/App/ErrorHandler.php:61"
Below is the array and json_encode
public function execute() {
foreach ($collection as $singleProduct):
$imageUrl = $imageUrl;
$html.='<div class="col-lg-3">';
$html.='<p class="img_small"><a href="'.$singleProduct->getProductUrl().'"><img class="custom-category-imgs" src="' . $imageUrl . '" title="' . $singleProduct->getName() . '"></a></p>';
$html.='<p class="cat_product"><a href="'.$singleProduct->getProductUrl().'">' . $singleProduct->getName() . '</a></p>';
$html.='<p><a class="btn_blue" href="'.$singleProduct->getProductUrl().'" title="View">View</a></p>';
$html.='</div>';
endforeach;
echo json_encode(array('html_data' => $html,'page_index' => $page));
}
and Ajax function is:
function load_product(category_id, page, current_obj) {
setTimeout(function () {
$.ajax({
method: "POST",
url: controller_url,
async: true,
data: {cat_id: category_id, p: page},
success: function (data) {
//alert(data);
var result = JSON.parse(data);
$("#insert_div" + category_id).html(result.html_data);
if (current_obj != 'default_val') {
setActivePageing(current_obj);
} else {
setActivePageing('default_val');
}
$("#wait").hide();
}
})
}, 2000);
}
Please help anybody this is the strange issue.