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

huangapple go评论110阅读模式
英文:

blank page or runtime error when serving page after handling form submission

问题

我能够加载一个名为index.html的HTML页面,其中<body></body>的内容如下所示:

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

我想要的是在渲染index.html后,当提交ask表单时,渲染一个来自我的golang代码的结果页面的index.html副本。

当我提交index.html中的表单时,我得到一个空白页面。

但是当我在浏览器中访问localhost:8000/view或localhost:8000/view?q=hello+world时,我得到了没有接收到数据的提示。在终端中,我得到了以下信息和更多内容,但这是第一行:

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

这是什么意思,我该如何修复它?

当我访问/ask或/ask?q=hello+world时,也就是我想要做的事情时,我得到了一个空白页面而没有错误。

我试图在加载的初始页面上处理ask,并在提交ask表单后在结果页面上处理它。

index.html、view.html和edit.html都有以下内容:

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

以下是我的代码:

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) {
    // Handle the response here
}

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()
}

所以我想要在index.html和结果页面上处理来自ask的请求,并运行response函数,该函数将在从index.html提交ask表单和结果页面时渲染结果页面。

英文:

I am able to load a html page ( index.html ) who's &lt;body&gt;&lt;/body&gt; contents looks like the following:

&lt;form action=&quot;ask&quot; method=&quot;get&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;q&quot; /&gt;
&lt;/form&gt;

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:

&lt;form method=&quot;get&quot; action=&quot;ask&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;q&quot; /&gt;
&lt;/form&gt;

Here is my code now:

package main
import (
&quot;fmt&quot;
&quot;net/http&quot;
&quot;github.com/zenazn/goji&quot;
&quot;github.com/zenazn/goji/web&quot;
&quot;html/template&quot;
&quot;io/ioutil&quot;
)
type Page struct {
Title string
Body []byte
}
func (p *Page) save() error{
filename := p.Title + &quot;.txt&quot;
return ioutil.WriteFile(filename, p.Body, 0600)	
}
func loadPage(title string) (*Page, error){
filename := title + &quot;.txt&quot;
body, err := ioutil.ReadFile(filename)
if err != nil{
return nil, err
}
return &amp;Page{Title: title, Body: body}, nil	
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page){
t, err := template.ParseFiles(tmpl + &quot;.html&quot;)
if err != nil{
fmt.Println(err)
}
t.Execute(w, p)
}
func viewHandler(w http.ResponseWriter, r *http.Request){
//title := r.URL.Path[len(&quot;/view/&quot;):]
title := r.FormValue(&quot;q&quot;)
p, err := loadPage(title)
if err != nil{
fmt.Println(err)
}
renderTemplate(w, &quot;view&quot;, p)
}
func editHandler(w http.ResponseWriter, r *http.Request){
//title := r.URL.Path[len(&quot;/edit/&quot;):]
title := r.FormValue(&quot;q&quot;)
p, err := loadPage(title)
if err != nil{
p = &amp;Page{Title: title}
fmt.Println(err)
}
renderTemplate(w, &quot;edit&quot;, 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(&quot;/&quot;, serveSingle(&quot;Projects/Go/src/web/site/index.html&quot;))
goji.Handle(&quot;/ask&quot;, response)
http.HandleFunc(&quot;/view/&quot;, viewHandler)
http.HandleFunc(&quot;/edit/&quot;, 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

得分: 0

请使用适当的错误处理,这样你就可以看到发生了什么情况。由于某种原因,它要么加载了一个空文件,要么无法找到该文件,这就是为什么结果页面是空白的原因。

如评论中所提到的:

renderTemplate函数中,使用panic(err)并检查t.Execute()的返回值。在viewHandler函数中也要使用panic(err)。在editHandler函数中同样要使用panic(err)

查看这些panic的信息,并从那里开始解决问题。

英文:

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.

huangapple
  • 本文由 发表于 2014年8月25日 02:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/25475003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定