gpt4 book ai didi

angularjs - 在 Phoenix/Elixir 中启用跨域资源共享 CORS

转载 作者:行者123 更新时间:2023-12-04 07:52:05 25 4
gpt4 key购买 nike

我的前端是一个单独的 Brunch.io AngularJS 应用程序。由于我的前端在 http://localhost:3333 上运行和我的 Phoenix 后端 http://localhost:4000尝试 POST 到 http://localhost:4000/api/users/register 时出现此错误

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 中发送 header ?

这是我的路由器.ex
  scope "/api", MyApp do
pipe_through :api
# Users
post "/users/register", UserController, :register
end

这是我的用户 Controller
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

最佳答案

您有几个选项可以为您连接 cors:
https://hex.pm/packages?search=cors&sort=downloads

关于angularjs - 在 Phoenix/Elixir 中启用跨域资源共享 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33555753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com