我能够加载一个 html 页面 ( index.html ) 谁是 <body></body>内容如下所示:
<form action="ask" method="get">
<input type="text" name="q" />
</form>
我正在尝试的是呈现 index.html,然后在提交请求时呈现 index.html 的副本,除了一些内容来 self 的 golang 代码作为结果页面。
当我在 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 时,这是我想要做的,我得到了没有错误的空白页面。
我正在尝试处理加载的初始页面和提交请求表单后的结果页面上的请求。
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){
}
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 和结果页面的询问并运行响应,这将在提交来自 index.html 和结果页面的询问表单时呈现结果页面。
最佳答案
使用正确的错误处理,您应该会看到发生了什么。由于某种原因,它要么正在加载一个空白文件,要么无法找到该文件,这就是结果页面为空白的原因。
如评论中所述:
在 renderTemplate 中,执行 panic(err) 并检查 t.Execute() 的返回值。在 viewHandler 中也执行 panic(err)。同样在 editHandler 中,执行 panic(err)。
看看那些 panic 怎么说,然后从那里开始。
关于go - 处理表单提交后提供页面时出现空白页面或运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475003/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我正在使用Postgres.app在OSX(10.8.3)上。我已经修改了我的PATH,以便应用程序的bin文件夹位于所有其他文件夹之前。Rammy:~phrogz$whichpg_config/Applications/Postgres.app/Contents/MacOS/bin/pg_config我已经安装了rvm并且可以毫无错误地安装pggem,但是当我需要它时我得到一个错误:Rammy:~phrogz$gem-v1.8.25Rammy:~phrogz$geminstallpgFetching:pg-0.15.1.gem(100%)Buildingnativeextension
文章目录git常用命令(简介,详细参数往下看)Git提交代码步骤gitpullgitstatusgitaddgitcommitgitpushgit代码冲突合并问题方法一:放弃本地代码方法二:合并代码常用命令以及详细参数gitadd将文件添加到仓库:gitdiff比较文件异同gitlog查看历史记录gitreset代码回滚版本库相关操作远程仓库相关操作分支相关操作创建分支查看分支:gitbranch合并分支:gitmerge删除分支:gitbranch-ddev查看分支合并图:gitlog–graph–pretty=oneline–abbrev-commit撤消某次提交git用户名密码相关配置g
我最近对我的计算机(OS-MacOSX10.6.8)进行了删除,并且我正在重新安装我所有的开发工具。我再次安装了RVM;但是,它不会让我安装Ruby1.9.3。到目前为止我已经尝试过:rvminstall1.9.3rvm安装1.9.3-p194rvm安装1.9.3-p448rvminstall1.9.3--with-gcc=clang所有返回相同的命令行错误:Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.6/x86_64/ruby-1.9.3-p448.Continuin
我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U