2016-12-06 156 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)