Last active
March 7, 2016 03:43
-
-
Save rubysolo/41186c7e56cd70af579b to your computer and use it in GitHub Desktop.
CORS with Phoenix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule App.Router do | |
use Phoenix.Router | |
resources "users", Controller.Users | |
options "/users", Controller.Users, :options | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Controller.Users do | |
use Phoenix.Controller | |
def index(conn) do | |
{:ok, user} = # ... get user JSON | |
json allow_cors(conn), user | |
end | |
def options(conn) do | |
text allow_cors(conn), "" | |
end | |
defp allow_cors(conn), do: put_resp_header(conn, "Access-Control-Allow-Origin", "*") | |
end |
@remiq Downcasing is no longer true. I've been able to do put_resp_header(conn, "Access-Control-Allow-Origin", "*")
without issue in Phoenix 1.1.4 :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Phoenix 0.13, you need to set header with lowercase:
defp allow_cors(conn), do: put_resp_header(conn, "access-control-allow-origin", "*")