2017-06-14 23 views
0

我在同一個Ubuntu Apache服務器上有兩個laravel項目。Laravel Apache Project中請求的資源上沒有'Access-Control-Allow-Origin'標頭

項目A可在www.EXAMPLE.com的test.EXAMPLE.com和「項目B」中找到。 「項目A」有一個網站,我向項目B發出了AJAX請求。在每個項目中,我都有一個GoDaddy SSL證書(一個用於test.EXAMPLE.com,另一個用於www.EXAMPLE.com)。

我得到的錯誤是:

XMLHttpRequest cannot load https://www.EXAMPLE.com/api_url. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://test.EXAMPLE.com' is therefore not allowed access. 

我該如何解決這個問題?我嘗試添加 「項目B」 裏面一個.htaccess與

Header set Access-Control-Allow-Origin "*" 

,並且還加入在 「Project A」 AJAX頭,這

'Access-Control-Allow-Origin' : '*', 
'Access-Control-Allow-Methods' : 'GET, POST, PUT, DELETE, OPTIONS' 

誰可以幫我,謝謝。

回答

0

安裝該軟件包:
https://github.com/barryvdh/laravel-cors

有了這個包,你可以很容易的添加CORS標頭,以你的選擇要求。
出於某種原因,我總是遇到與Cors標題有關的問題。但他們應該只是在那裏爲每個選項請求完成一個Ajax調用。

0

您可以laravel創建一個簡單的中間件:

<?php 

namespace App\Http\Middleware; 

use Closure; 

class CorsHeaders 
{ 
    /** 
    * Append CORS headers to response 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     $response = $next($request); 

     $response->header('Access-Control-Allow-Origin', '*'); 
     // add allowed headers if needed 
     // $response->header('Access-Control-Allow-Headers', ''); 
     $response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 

     return $response; 
    } 
} 

並在HTTP內核$middleware陣列註冊。

+0

就我而言,這並沒有改變任何東西。我將這個中間件添加到我所調用的路由中,問題仍然存在。 –

+0

關於什麼?實際的電話或預檢請求? – Robert

相關問題