第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用jquery表單提交時如何從golang重定向到另一個模板?

使用jquery表單提交時如何從golang重定向到另一個模板?

Go
Qyouu 2022-01-04 21:10:10
我正在使用表單提交 jquery 到 html 頁面。數(shù)據(jù)成功傳送到我的后端 beego(golang framework) 控制器。運行某些功能后,控制器必須決定下一步重定向的位置。但這被jquery阻止了。如何使用 go 代碼覆蓋 jquery 和重定向?我的劇本$().ready(function() {    $("#signupForm").validate({        rules: {            fullname: "required"                        },        messages: {            fullname: "Please enter your full name"        },        submitHandler: function(form){            $(form).ajaxSubmit({                    url: '/add_user',                    type: 'POST',                    dataType: 'html',                    data : $( "#signupForm" ).serialize(),                    success : function(data) {                    }            });            return false        }    });});我的代碼status := user.AddUserAndCompany(company)switch status {case true:    this.Data["Website"] = "beego.me"    this.Data["Email"] = "astaxie@gmail.com"    this.TplName = "index.tpl"case false:    this.Data["ErrorMessage"] = "ServerConnectionError"    this.Layout = "layouts/header_logout.html"    this.TplName = "templates/registration.html"}
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經(jīng)驗 獲得超7個贊

我最終做的并不是一個完美的方法。但它解決了我的問題。所以我正在分享它。我歡迎針對此問題的所有建議和任何其他解決方案。


switch dbStatus {

    case 0://db Error

        dataToClient = "Server Connection Error"

        this.Ctx.ResponseWriter.Write([]byte(dataToClient)) //Error_msg_1

    case 1://Email already in use

        dataToClient = "EmailError"

        this.Ctx.ResponseWriter.Write([]byte(dataToClient)) //Error_msg_2

    case 2://UserName already in use

        dataToClient = "UserNameError"

        this.Ctx.ResponseWriter.Write([]byte(dataToClient)) //Error_msg_3

    case 3://Everything is fine

        if user.UserType == 0 { //Choosing the url

            dataToClient = "/admin_home" //url_1

            this.Ctx.ResponseWriter.Write([]byte(dataToClient))

        } else {

            dataToClient = "/user_home" //url_2

            this.Ctx.ResponseWriter.Write([]byte(dataToClient))

        }

    }

我沒有成功找到從后端重定向的方法。所以我編碼從后端選擇url路徑并將其發(fā)送到前端this.Ctx.ResponseWriter.Write([]byte(dataToClient))


現(xiàn)在在前端我做了以下事情:


$("#signupForm").validate({

            rules: {

                fullName: "required"

            },

            messages: {

                fullName: "Please enter your full name"

            },

            submitHandler: function(form){

                $(form).ajaxSubmit({

                        url: '/add_user',

                        type: 'POST',

                        dataType: 'html',

                        data : $( "#signupForm" ).serialize(),

                        success : function(data) {

                            //Checking whether the data from back-end is an url or not... 

                            if (data.charAt(0) === '/'){ // if url

                                window.location.href = data;

                            }else{ //else error message

                                document.getElementById('regError').innerHTML = data;

                            }


                        }

                });

                return false

            }

        });

    });


查看完整回答
反對 回復(fù) 2022-01-04
?
慕田峪4524236

TA貢獻1875條經(jīng)驗 獲得超5個贊

你不能用 c.Redirect(redirectURL, 302) 重定向到一個全新的控制器,其中 'c' 是你的控制器嗎?

我基本上對“/”、“/register”等使用不同的控制器。


查看完整回答
反對 回復(fù) 2022-01-04
  • 2 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號