I´m working on some controllers in OpenCart 3 and I want one of the controller functions only to be accessible through AJAX request. I tried with $_SERVER['HTTP_X_REQUESTED_WITH'] variable but OC says that the variable is undefined.
Best Regards
pcosta94
actually OpenCart doesn't remove the $_SERVER vars. It just passes them to $this->server. The issue is that 'HTTP_X_REQUESTED_WITH' is only set if there was an AJAX call. Otherwise, it will give an error.
you can test if any of your requests have been made via AJAX by adding this code to file system/library/request.php
on line 32
after $this->server = $this->clean($_SERVER);
add:
if(isset($this->server['HTTP_X_REQUESTED_WITH'])){
echo '<script>console.log(' . json_encode($this->server['HTTP_X_REQUESTED_WITH']) .')</script>';
}
then open your OpenCart frontend and visit any product page and you should see in your Browser console XMLHttpRequest
like so http://joxi.ru/MAjo6vWTjZZjBr
this is because on the product page the reviews are loaded via AJAX. On homepage there are no AJAX calls, so you shouldn't see anything in the console.
I hope this helps.