On LAN, backend is laravel 5.2 (domain http://api.angular.ng/ ), in the .htaccess file I wrote:

Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE" Header set Access-Control-Allow-Headers "Origin, Content-Type, X-Auth-Token" 

At the front stands angular2. POST I transfer request for backend with hederami:

 let headers = new Headers({ 'Content-Type': 'application/json' }); 

As a result, I get 500 error:

General:

 Request URL:http://api.angular.ng/register Request Method:POST Status Code:500 Internal Server Error Remote Address:127.0.0.1:80 

Response Headers:

 Access-Control-Allow-Headers:Origin, Content-Type, X-Auth-Token Access-Control-Allow-Methods:GET, POST, PUT, DELETE Access-Control-Allow-Origin:* Cache-Control:no-cache, private Connection:close Content-Type:text/html; charset=UTF-8 Date:Wed, 04 Jan 2017 08:18:23 GMT Server:Apache/2.4.23 (Unix) PHP/5.6.27 Set-Cookie:laravel_session=eyJpdiI6IkNZbFhUVkZjVU9pYmR2RjAxQXNpcUE9PSIsInZhbHVlIjoiNUFnYTU1TkhRXC9HNTZWbXAwOHpPNE0xYzNxcjloTnlZUnNyM0NZTmN3Y0hUY3czTlwvdXJWT1pIZnNIUGpFVEJUNHA2SlZyYktYT3VPUERJVERkQkJhQT09IiwibWFjIjoiOGFmMzRmMjY2ZWQzMDhlNjc2MDQwYzQ4NTAxM2I3Mjg3MWMzYWJhZDQzM2I5M2JiZTY3NDE1ZmZkYWU1MDAwNSJ9; expires=Wed, 04-Jan-2017 10:18:23 GMT; Max-Age=7200; path=/; httponly Transfer-Encoding:chunked X-Powered-By:PHP/5.6.27 

Request Headers:

 Accept:*/* Accept-Encoding:gzip, deflate Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4 Connection:keep-alive Content-Length:15 Content-Type:application/json Host:api.angular.ng Origin:http://localhost:3000 Referer:http://localhost:3000/login 

Tell me, please, who met with the like?

  • And what error description? - Orange_shadow
  • @Orange_shadow POST api.angular.ng/register 500 (Internal Server Error) - Dzedzik

1 answer 1

I usually do this middleware

 <?php namespace App\Http\Middleware; use Closure; class Cors { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $domains = []; if (isset($request->server()['HTTP_ORIGIN'])) { $origin = $request->server()['HTTP_ORIGIN']; if (in_array($origin, $domains)) { header('Access-Control-Allow-Origin: '.$origin); } header('Access-Control-Allow-Headers: Origin, Content-Type, Authorization'); } return $next($request); } } 

And you need to insert here: App \ Http \ Kernal.php;

 protected $routeMiddleware = [ ..., 'cors' => \App\Http\Middleware\Cors::class, ] 

And use this:

 Route::get('/user', function (Request $request) { return $request->user(); })->middleware('cors'); 
  • Well, in fact, in middleware, what I have in .htaccess is done. I did the same, added a group to the routes, but for some reason Laravel did not give up those heders. therefore made through .htaccess - Dzedzik
  • So did you connect the middleware to the route? See corrected, connection example - Orange_shadow
  • of course added. in Kernel.php in $ routeMiddleware added the line 'cors' => \ App \ Http \ Middleware \ Cors :: class. Then he created Route :: group (['middleware' => 'cors'], function () {...}) in routs - Dzedzik
  • the truth is a little wrong with my registered. I will try to do what you do - Dzedzik
  • @Dzedzik So what's the mistake that you have 500 (Internal Server Error) is good? But is there a description? There you can have a problem in general with syntax somewhere? Do you normally refer to other routes? only when is the mistake here? - Orange_shadow