2014-10-28 42 views
0

最後我決定張貼...我覺得白癡,很多信息,我也沒有找到答案,的Symfony2 + AngularJS + CORS

很好,問題是在CORS,我試着做這angularJS:

var request = $http({ 
     method: "POST", 
     url: url, 
     data: data 
    }); 

URL是用symfony的做JsonResponse(),沒有一個服務器特殊,與郵差嘗試它,它是所有罰款(所以..服務器是好?),但是當我嘗試與其他網絡我在控制檯看到:

XMLHttpRequest cannot load http://server/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 405. 

其實在symfony中我有NelmioBundle與配置:

nelmio_cors: 
    defaults: 
     allow_credentials: true 
     allow_origin: ['*'] 
     allow_headers: ['*'] 
     allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS'] 
     max_age: 3600 
    paths: 
    '^/': 
     allow_origin: ['*'] 
     allow_headers: ['*'] 
     allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS'] 
     max_age: 3600 

我使用LEMP瞭解更多信息,

感謝提前和我verybad英語對不起!

+0

此鏈接您還沒有定義'在默認或路徑host'。 – hcoat 2014-10-29 01:48:45

回答

0

我看到你已經設置了allow_credentials: true。您還應該在Angular中設置相關的HTTP標頭。

$http.defaults.headers.post['Access-Control-Allow-Credentials'] = 'true'; 
var request = $http.post(path, data, { withCredentials: true }); 

有關CORS的更多信息,請參閱CORS tutorial