ArleyR
Problem with CORS - request failed, has been blocked by CORS policy
I’m doing requests with Graphql and Apollo Client to a backend project made with Phoenix. I had a problem because the request failed and the Chrome console shows me the error “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.”
I used CORSICA and CORSPlug to allow access but it didn’t work. Please, anyone has an example or solved that error?
Thanks!
Most Liked
kokolegorille
How did You configure CORS? I had to update my endpoint to make it work (with Corsica).
Something like this…
plug Corsica,
origins: [
"http://localhost:4000",
"http://localhost:8000",
"http://localhost:8080"
],
allow_headers: ["accept", "content-type", "authorization"],
allow_credentials: true,
log: [rejected: :error, invalid: :warn, accepted: :debug]
Of course your configuration may vary a lot…
ArleyR
Hello @dumadi, your API use Elixir? In my case, I solved that problem using [cors_plug]. I had an umbrella project (as my API), and I put that plug inside the project with the endpoint.
I modified the Router, something like this:
defmodule APIWeb.Router do
use APIWeb, :router
pipeline :any_name do
plug CORSPlug, origin: "*"
...
end
scope "/" do
...
pipe_through(:any_name)
...
end
end
If you have an API in another language, you need to investigate how to configure the cors.
I used this tutorial for express: cors middleware · Express.js
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









