2016-12-06 178 views
1

我正在向帶有HTML表單的服務器發送發佈請求。當我按下表單的提交按鈕時,我得到一個404頁面不存在的錯誤。但是,如果我通過直接在地址欄中輸入內容來訪問URL,則會發現我應該從服務器獲得的錯誤(400無效請求)。頁面存在時出現404錯誤

這裏是我的HTML(注意我用的葉):

#extend("base") 

#export("head") { 
    <link rel="stylesheet" href="/styles/normalize.css"> 
    <link rel="stylesheet" href="/styles/skeleton.css"> 
} 

#export("body") { 
    <div class="container"> 
     <form action="/admin/new-post" method="POST"> 
      <div class="row" style="padding: 25px 0 0 0;"> 
       <div class="six columns form-group"> 
        <label for="email">Usernam</label> 
        <input class="u-full-width" type="text" placeholder="Username" id="email" name="email"> 
       </div> 
       <div class="six columns form-group"> 
        <label for="password">Password</label> 
        <input class="u-full-width" type="password" placeholder="Password" id="password" anem="password"> 
       </div> 
      </div> 
      <input type="hidden" name="_csrf" value="{{csrfToken}}"> 
      <button type="submit" class="button-primary">Login</button> 
     </form> 
    </div> 
} 

我的服務器代碼(SWIFT):

import Vapor 
import HTTP 

final class AdminController { 

    func addRoutes(to drop: Droplet) { 
     let admin = drop.grouped("admin") 
     drop.get("login", handler: login) 
     admin.get("new-post", handler: newPost) 
    } 

    func login(_ request: Request)throws -> ResponseRepresentable { 
     return try drop.view.make("admin-login", []) 
    } 

    func newPost(_ request: Request)throws -> ResponseRepresentable { 
     guard let cred = request.auth.header?.basic else { 
      throw Abort.badRequest 
     } 
     do { 
      try request.auth.login(cred) 
      return try drop.view.make("new-post", []) 
     } catch { 
      return Response(redirect: "/login?login=failed") 
     } 
    } 
} 

爲什麼我收到一個404錯誤?

我使用的Web框架是Vapor。

回答

2

所以,看看你的小滴,你添加新的路由與GET - 請求new-post但你的形式嘗試POSTnew-post,肯定會失敗。

試圖改變

admin.get("new-post", handler: newPost)

admin.post("new-post", handler: newPost)