【问题标题】:Symfony2 + AngularJS + CORSSymfony2 + AngularJS + CORS
【发布时间】:2014-11-26 14:45:52
【问题描述】:

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

好吧,问题出在 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 获取更多信息,

感谢您的提前,抱歉我的英语很糟糕!

【问题讨论】:

  • 您没有在默认值或路径中定义host

标签: angularjs symfony cors


【解决方案1】:

我看到你设置了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 上的此链接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 2022-01-26
    • 1970-01-01
    • 2021-08-11
    • 2013-12-03
    • 2015-04-12
    • 2013-11-02
    • 2013-07-19
    相关资源
    最近更新 更多