I'm making an API, and locally it's perfect. The problem is when I test it on the server.
I created it in a subdomain, example: link And I'm testing on another subdomain: link
Locally the addresses look like this:
Note: The folder structure is the same on the server. When I make an invalid request locally the API returns me a structure like this:
{
"error": 401,
"message": "Permissão negada."
}
But when I test on the server AJAX does not return anything, only the following error is displayed on the console:
BeforeIwasredirectingtotheControllerandActionerror:
self::$controller->request->redirect(Route::href('error/unauthorized'));
ThenIthoughtthatredirectsdidnotworkwithAJAXrequests,soIchangedthestructuretoprintouttheControllerandActionresults:
exit(self::$controller->controller('Error','unauthorized'));
Butitcontinuedthesameway.
Ihavealreadyputtheservertoprinttheerrors:
ini_set('display_startup_errors',1);ini_set('display_errors',1);error_reporting(E_ALL);
AndtoacceptCORSrequests:
header('Access-Control-Allow-Origin:*');
Butnothingisreturnedfromtheserver.
UPDATE
Myapplicationrunsthefollowingcode:
publicstaticfunctionvalidateAccess(){if(!self::validateApplication()){exit('Estáentrandoaqui');self::$controller->request->redirect(Route::href('error/unauthorized'));}}
Theexit
aboveisdisplayed,ifIremoveitandputitonthecontrolleroftheredirect,itisnolongerdisplayed:
classErrorControllerextendsController{publicfunctionunauthorized(){exit('unauthorized');returnself::returnError('Unauthorized');}}
Theredirect
methodhasthefollowingcode:
publicfunctionredirect($params=NULL){if(empty($params))return$this->header('Location:'.Route::href());if(is_string($params))return$this->header('Location:'.$params);if(is_array($params)){$controller=empty($params['controller'])?'main':$params['controller'];$action=empty($params['action'])?'index':$params['action'];return$this->header('Location:'.Route::href("{$controller}/{$action}"));
} else
return $this->header('Location: ' . Route::href());
exit;
}
public function header($content) {
header($content);
}