PHP Works Local (XAMPP - PHP 5.5.33) but does not work on Server (PHP 5.5.31)

2

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:

API: link Test: link

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'));}}

Theexitaboveisdisplayed,ifIremoveitandputitonthecontrolleroftheredirect,itisnolongerdisplayed:

classErrorControllerextendsController{publicfunctionunauthorized(){exit('unauthorized');returnself::returnError('Unauthorized');}}

Theredirectmethodhasthefollowingcode:

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);
}
    
asked by anonymous 14.03.2016 / 20:46

2 answers

3

After several tests I noticed that redirection does not work with Ajax requests, probably for some security reason, when the browser receives the code 301 redirection it already stops the request.

The solution found was to terminate the script with Controller to which it would redirect:

exit(self::$controller->controller('Error', 'unauthorized'));
    
15.03.2016 / 13:57
1

Your post has long been, but it is for those who enter in the future. By default browsers block cross domain requests, and this is what happened to you.

You requested in api.domain being tested.api.domain.

An alternative to allowing cross domain ajax requests is to use jsonp instead of json. jsonp is very similar with json with the difference that it is encapsulated in a function and when it arrives the browser runs to get the data. This type of data browsers allow you to do cross-domain.

Generally when building an api that returns jsonp it is convenient to allow the client to send in the body of the request as parameter the name padding example:

link

the return will be something like:

foo ({"Name": "Otto", "Id": 3, "Age": 30});

In this way the client can determine what he needs for the correct parse.

    
11.11.2016 / 11:40