2017-02-03 7 views
0

私はangle js 1.5とlaravel 5.4で作業しています。フロントエンドとバックエンドに使用されるLaravelの角度使用時のCORSヘッダーエラー

私はCORSのlaravelにミドルウェアを作成しました。それは以下のようになります。

public function handle($request, Closure $next) 
{ 
    header("Access-Control-Allow-Origin: *"); 
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); 
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With'); 
    header('Access-Control-Allow-Credentials: true'); 

    if (!$request->isMethod('options')) { 
     return $next($request); 
    } 
} 

、角度JSで、私はそれが完璧に動作するように、ベアラトークンを取得するためのPOSTリクエストを作成しました。私は応答のトークンを得た。それから私はセッションにそれを保存し、すべての要求のヘッダーにトークンを設定するためにインターセプターを使用します。

のように私のコントローラはなります。私の工場は、以下のように見える

app.controller('LoginController',function($scope,$http,$window,$state) { 
$scope.vm.login = function() { 

    $http({ 
      method: 'POST', 
      url: apiUrl+'auth/token', 
      data: $.param($scope.vm.logDetails), 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
     }).then(function(response) { 
      if(response.data.access_token) { 
       $window.sessionStorage.setItem('userInfo-token', response.data.access_token); 
       $state.go('dashboard.masters_userroles');     
      }    
     }); 
    } 
}); 

.factory('tokenInjector',function($window){ 
return { 
    //For each request the interceptor will set the bearer token header. 
    request: function($config) { 

     if($window.sessionStorage.getItem('userInfo-token')) 
     { 
      var token=$window.sessionStorage.getItem('userInfo-token'); 
      console.log(token); 
      //set authorization header 
      $config.headers['Accept'] = 'application/json' 
      $config.headers['Authorization'] = 'Bearer '+token; 

     } 
     return $config; 
    } 
} 
}) 
.config(function ($httpProvider) { 
    $httpProvider.interceptors.push('tokenInjector'); 
}) 

それは$state.go('dashboard.masters_userroles');この状態を呼び出したときに、それが

MLHttpRequest cannot load http://api.local.support.com/api/masters/userroles?page=1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://local.support.com' is therefore not allowed access.

答えて

1

のようなエラーが発生しますこのパッケージを使ってみてください。それVEの:私はこのパッケージが後

+0

あまりにも感謝の仕事を自分のミドルウェアを作る失敗使う

https://github.com/barryvdh/laravel-cors

。しかし、なぜ私のcorsミドルウェアが動作していないのか分かりますか?そして、GETメソッドを使用するとき、角度jでOPTIONに変換されるGETメソッドを防ぐ方法 –

関連する問題