【问题标题】:Getting a 404 error when the page exists页面存在时出现 404 错误
【发布时间】:2016-12-06 15:08:31
【问题描述】:

我正在使用 HTML 表单向服务器发出 post 请求。当我按下表单的提交按钮时,我得到一个 404 页面不存在错误。但是,如果我通过直接在地址栏中输入来访问 URL,则会收到我应该从服务器获取的错误(400 无效请求)。

这是我的 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 错误?

我使用的网络框架是 Vapor。

【问题讨论】:

    标签: html swift post http-status-code-404 vapor


    【解决方案1】:

    所以,看看你的 droplet,你添加了一个带有 GET-Request new-post 的新路由,但是你的表单尝试 POSTnew-post,对于肯定会失败。

    尝试改变

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

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

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      相关资源
      最近更新 更多