2015-11-05 118 views
7

我的前端是一个单独的Brunch.io AngularJS应用程序。由于我的前端上http://localhost:3333运行和http://localhost:4000我的凤凰后端我尝试后到http://localhost:4000/api/users/register在Phoenix/Elixir中启用跨源资源共享CORS

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 "https://stackoverflow.com/users/register", UserController, :register 
    end 

这是我UserController的

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 

回答