#CORS

6 messages · Page 1 of 1 (latest)

gleaming gust
#

I'm trying to configure cors for my Phoenix app - when I submit my API request I receive the response:

Access to XMLHttpRequest at 'http://localhost:4000/api/data' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

In my mix.exs file I include the dep: {:cors_plug, "~> 3.0"}

In my router.ex file I have:

pipeline :api do
    plug :accepts, ["json"]
    plug CORSPlug, origin: ~r/https?.*localhost.\d*/

    forward "/", Absinthe.Plug, schema: DxAppRcWeb.Schema
  end

Any ideas what I could be doing wrong?

#

Also, when I replace the regex string with *, I get the same fault

paper wind
#

Do you mean ".*" ?

#

Your original regex was https but the error says http

gleaming gust
#

I think the problem was though that the router in the endpoint.ex file was before the pipeline where I used the cors plug, which meant that the router was rejecting the request before it ever got processed