dongwo5589 2014-08-24 18:33
浏览 181
已采纳

处理表单提交后提供页面时出现空白页或运行时错误

I am able to load a html page ( index.html ) who's <body></body> contents looks like the following:

<form action="ask" method="get">
    <input type="text" name="q" />
</form>

What I am trying to so is render index.html and then on submission of the ask render a copy of index.html except have some of the contents come from my golang code as a results page.

When I submit the form in index.html, I get a blank page.

But I am getting no data received when going to localhost:8000/view or localhost:8000/view?q=hello+world in the browser. And in the terminal, I get this and a lot more, but this is the first line:

http: panic serving [::1]:53803: runtime error: invalid memory address or nil pointer dereference

what does it mean and how can I fix it?

WhenI go to /ask or /ask?q=hello+world, which is what I am trying to do, I get the blank page with no errors.

I am trying to handle ask on the initial page that is loaded and on the results page once that ask form has been submitted.

index.html, view.html, and edit.html all have this:

<form method="get" action="ask">

    <input type="text" name="q" />

</form>

Here is my code now:

package main

import (

    "fmt"
    "net/http"
    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"

    "html/template"
    "io/ioutil"
)

type Page struct {

    Title string

    Body []byte

}

func (p *Page) save() error{

    filename := p.Title + ".txt"

    return ioutil.WriteFile(filename, p.Body, 0600) 

}

func loadPage(title string) (*Page, error){

    filename := title + ".txt"

    body, err := ioutil.ReadFile(filename)

    if err != nil{

        return nil, err

    }

    return &Page{Title: title, Body: body}, nil 

}

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page){

    t, err := template.ParseFiles(tmpl + ".html")

    if err != nil{
        fmt.Println(err)
    }
    t.Execute(w, p)

}

func viewHandler(w http.ResponseWriter, r *http.Request){

    //title := r.URL.Path[len("/view/"):]

    title := r.FormValue("q")

    p, err := loadPage(title)

    if err != nil{
        fmt.Println(err)
    }

    renderTemplate(w, "view", p)

}

func editHandler(w http.ResponseWriter, r *http.Request){

    //title := r.URL.Path[len("/edit/"):]

    title := r.FormValue("q")

    p, err := loadPage(title)

    if err != nil{

        p = &Page{Title: title}
        fmt.Println(err)

    }

    renderTemplate(w, "edit", p)

}

func response(c web.C, w http.ResponseWriter, r *http.Request){

}

func serveSingle(filename string) func(w http.ResponseWriter, r *http.Request) {

    return func(w http.ResponseWriter, r *http.Request) {

        http.ServeFile(w, r, filename)

    }

}

func main() {

    goji.Get("/", serveSingle("Projects/Go/src/web/site/index.html"))

    goji.Handle("/ask", response)

    http.HandleFunc("/view/", viewHandler)

    http.HandleFunc("/edit/", editHandler)

    goji.Serve()

}

So I want to handle ask from the index.html and the results page and run response, which will render the results page on submission of the ask form from index.html and the results page.

  • 写回答

1条回答 默认 最新

  • douxingsuo8809 2014-08-24 22:01
    关注

    Use proper error handling and you should see what is going on. For some reason it is either loading a blank file or no able to find the file and that is why the results page is blank.

    As mentioned in the comments:

    In renderTemplate, do a panic(err) and check the return value of t.Execute(). In viewHandler do a panic(err) as well. Same in editHandler, do a panic(err).

    See what those panics say and go from there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器