我這是在鳳凰app,其中註銷按鈕是在鳳凰城的模板,如通過以下三個代碼片段建立一個註銷系統。這工作正常。鳳凰JSON API - 配置路由調用正確的函數在控制器
鳳凰模板:/web/templates/layout/app.html.eex
<%= link "Logout", to: auth_path(@conn, :delete), method: :delete, class: "btn btn-danger" %>
鳳凰路線:/web/router.ex
的相關的路線是刪除路線。
scope "/auth", AdminApi do
pipe_through :browser
get "/:provider", AuthController, :index
get "/:provider/callback", AuthController, :callback
delete "/logout", AuthController, :delete
end
鳳凰控制器:/web/controllers/auth_controller.ex
def delete(conn, _params) do
conn
|> put_flash(:info, "You have been logged out!")
|> configure_session(drop: true)
|> redirect(to: "/")
end
我想實現同樣的事情,但一個單獨的前端和使用P作爲JSON API的hoenix。
的想法是,前端使一個AJAX DELETE請求相關的API端點,然後路線抓住這一呼籲並觸發在auth_controller刪除功能。
如何配置路由,使之火在控制器中的相關功能?
PS我敢肯定它不是一個CORS問題,因爲CORS插頭正在爲其他途徑,例如「/ auth /中:供應商」
下面是我到目前爲止有:
從前端var url = ENV.apiProtocol + ENV.apiHost + "/api/auth";
Ember.$.ajax({
url : url,
type: "DELETE",
success: function(response){
console.log(response)
},
error: function(response) {
console.log(response)
}
});
AJAX請求條
鳳凰路線:/web/router.ex
scope "/api", AdminApi do
pipe_through: api
resources "/auth/:provider", AuthController, except: [:new, :edit]
resources "/auth", AuthController, except: [:new, :edit]
end
鳳凰控制器:/web/controllers/auth_controller.ex
def delete(conn, _params) do
conn
|> put_flash(:info, "You have been logged out!")
|> configure_session(drop: true)
|> redirect(to: "/")
end
這是什麼部分不起作用?你有沒有可以分享的錯誤? –
錯誤說「找不到DELETE/api/auth(AdminApi.Router)的路由」 – AJP
如果您運行'mix phx.routes',那裏是否列出了?你有沒有嘗試重新啓動你的鳳凰服務器? –