【问题标题】:Enabling Cross-Origin Resource Sharing CORS in Phoenix / Elixir在 Phoenix / Elixir 中启用跨域资源共享 CORS
【发布时间】:2016-02-06 22:45:48
【问题描述】:

我的前端是一个单独的 Brunch.io AngularJS 应用程序。由于我的前端在 http://localhost:3333 上运行,而我的 Phoenix 后端在 http://localhost:4000 上运行,因此在尝试 POST 到 http://localhost:4000/api/users/register 时出现此错误

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3333' is therefore not allowed access. The response had HTTP status code 404.

所以我认为这是一个 CORS 问题。如何在 phoenix 中发送标头?

这是我的 router.ex

  scope "/api", MyApp do
    pipe_through :api
    # Users
    post "/users/register", UserController, :register
  end

这是我的用户控制器

defmodule MyApp.UserController do
  use MyApp.Web, :controller

  def register(conn, params) do
    IO.puts(inspect(params))

    conn
    |> put_status(201)
    |> json  %{ok: true, data: params}
  end

end

【问题讨论】:

    标签: angularjs cors elixir phoenix-framework


    【解决方案1】:

    您有几个选项可以为您连接 cors: https://hex.pm/packages?search=cors&sort=downloads

    【讨论】:

    • 谢谢。我让它与 plug_cors 一起工作,但会看看 corsica
    猜你喜欢
    • 2013-01-12
    • 2016-05-01
    • 2018-05-04
    • 2014-01-18
    • 2014-10-08
    • 2014-03-20
    • 2016-05-31
    • 2011-06-10
    相关资源
    最近更新 更多