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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。