drl47263 2018-12-09 17:17
浏览 14

由于没有HTTP重定向,导致执行模板提交问题?

I am trying to pass data from one page to another utilizing go. Here is a simplified version of what I am doing:

//Attempting to go from page1.gohtml to page2.gohtml to page3.gohtml.

type myData stuct {
    data1 string
    data2 string
}

func page1(w http.ResposneWriter, r *http.Request) {
    if r.Method != http.MethodPost {
        myTemplate.ExecuteTemplate(w, "page1.gohtml", my_data) //do current page
    }

   //populated myData

    myTemplate.ExecuteTemplate(w, "page2.gohtml", my_data) //go to next page
}

//My .gohtml forms are like so:
<form id="page1" name="page1" method="post">
    <input type="submit" class="click">
</form>

Repeat general code for page 2 to page 3.

Problem Statement: When I do template execution from page 1 to page 2, my data struct remains populated, the page however, remains on localhost:80/page1

It does not move to page 2 despite displaying page 2.

This isn't a problem until I go to execute the submit on page 2. This brings me back to page 1 instead of taking me to page 3.

I have tried setting my form with an action taking to take me to the next page. This redirects me, but does not bring me the populated my data structure that I need.

Setting the submit to return false stops my server code from executing.

And doing an HTTP Redirect brings me to the next page but has the same issue as the form action.

This is temporary data that I do not want to save to a Database.

Could someone help direct me on my options?

  • 写回答

1条回答 默认 最新

  • duanji9264 2018-12-09 17:44
    关注

    As I see it, you don't redirect, you just execute the page2 template.

    For redirecting, try:

    http.Redirect(w, r, "http://127.0.0.1/page2", 301)
    

    instead of

    myTemplate.ExecuteTemplate(w, "page2.gohtml", my_data) //go to next page
    

    edit:

    I see now that I didn't fully answered your question, since I ignored my_data (which by the way, in golang the named are camelCase, so you should rename it with myDate... :).

    Anyway, for passing the data, since it's only strings, you can try to serialize it and add it to the request body (I don't know if it's the best practice, it's just the first thing that pop into my head that will probably work)

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比