Symfony2 + AngularJS + CORS

时间:2014-10-28 19:21:49

标签: angularjs symfony cors

最后我决定发布那个...我觉得很白痴,很多信息都没有找到答案,

好吧,问题在于CORS,我尝试在angularJS中执行此操作:

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

url是一个服务器,有一个symfony做一个JsonResponse(),没什么特别的,尝试用POSTMAN一切都很好(所以..服务器很好吗?)但是当我尝试使用其他网络时我在控制台中看到:

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获取更多信息,

感谢您提前和抱歉我的英语!

1 个答案:

答案 0 :(得分: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

上的此链接