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

标签 angularjs cors elixir phoenix-framework

我的前端是一个单独的 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/

相关文章:

javascript - E2E 测试、单元测试和使用 $httpBackend 模拟数据之间的混淆

javascript - 通过 ng-repeat 单击访问 li 数据

django - 更改商店的 URL 时,ExtJS 5 应用程序 + Django rest 框架 CORS 错误

java - 在 Spring Boot 中全局启用 CORS

elixir - ecto 中的虚拟模型

websocket - 测试客户端 channel 是否收到消息

javascript - 为什么我的 AngularJS View 无法在 Cordova 中加载?

javascript - Canvas 被跨源数据污染

带有命名文件的 HTTP POST 多部分

angularjs - 在 Angular nvd3 中对鼠标悬停使用不同的值