dongping1689 2017-12-30 19:26
浏览 153

在Iris Framework中以POST方法重定向

I'm trying to redirect from the login page to the home page in the POST method, but the form doesn't redirect to any page after clicking on the submit button.

I'm using the MVC structure of Iris Framework and the Ctx.Redirect method to redirect to the desired page, but it's not working.

// login_controllers.go
package controllers

import (
    "github.com/kataras/iris/mvc"
    "github.com/kataras/iris"
    "fmt"
)

type LoginFormData struct {
    Email string
    Password string
}


type LoginController struct {
    mvc.C
}

func (c *LoginController) Get() mvc.Result {
    return mvc.View{
        Name: "login.html",
    }
}

func (c *LoginController) Post() {
    userLoginData := LoginFormData{}
    err := c.Ctx.ReadForm(&userLoginData)
    if err != nil {
        c.Ctx.StatusCode(iris.StatusInternalServerError)
        c.Ctx.WriteString(err.Error())
    }

    if userLoginData.Email == "teste@teste.com" && userLoginData.Password == "123" {
        c.Ctx.Redirect("/", iris.StatusSeeOther)

        fmt.Printf("UserInner: %#v", userLoginData)
        c.Ctx.Writef("UserInner: %#v", userLoginData)
    }
}

The form HTML code is:

<form class="m-login__form m-form" action="/login" method="post">
    <div class="form-group m-form__group">
        <input class="form-control m-input" type="text" placeholder="Email" name="Email" autocomplete="off">
    </div>
    <div class="form-group m-form__group">
        <input class="form-control m-input m-login__form-input--last" type="password" placeholder="Password" name="Password">
    </div>
    <div class="row m-login__form-sub">
        <div class="col m--align-left">
            <label class="m-checkbox m-checkbox--focus">
            <input type="checkbox" name="remember">
                Remember me
            <span></span>
            </label>
        </div>
        <div class="col m--align-right">
            <a href="javascript:;" id="m_login_forget_password" class="m-link">
            Forgotten password ?
            </a>
        </div>
    </div>
    <div class="m-login__form-action">
        <button id="m_login_signin_submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air">
        Login
        </button>
    </div>
</form>

Isn't possible to redirect in a POST method in Iris Framework/Go?

  • 写回答

1条回答 默认 最新

  • dongshui9690 2017-12-31 18:21
    关注

    As @mkopriva said you have to call .Redirect and return from the function body, Redirect should be the last call because it sends the status code and a header location back to the client, you don't have to write anything else, browser/client will redirect and write the content of the redirect path.

    Although iris.StatusSeeOther (http status code: 303) works on the mvc examples the iris repository has, you may want to try other statuses and check if that works (clear your browser's cache on each try for any case), for example iris.StatusTemporaryRedirect (http status code: 307) or search on google for the correct redirect status code that suits your needs better.

    Please take a look at the https://github.com/kataras/iris/tree/master/_examples as well.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法