Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Last active March 7, 2016 03:43
Show Gist options
  • Save rubysolo/41186c7e56cd70af579b to your computer and use it in GitHub Desktop.
Save rubysolo/41186c7e56cd70af579b to your computer and use it in GitHub Desktop.
CORS with Phoenix
defmodule App.Router do
use Phoenix.Router
resources "users", Controller.Users
options "/users", Controller.Users, :options
end
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
Copy link

remiq commented Nov 19, 2015

In Phoenix 0.13, you need to set header with lowercase:
defp allow_cors(conn), do: put_resp_header(conn, "access-control-allow-origin", "*")

@wulymammoth
Copy link

@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