英文:
GO: javascript and html MIME error
问题
我正在尝试在我的GO应用程序中加载JavaScript,但是我遇到了错误:
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:4747/twttr.js". localhost/:6
Uncaught SyntaxError: Unexpected token <
GO代码:
package main
import (
"net/http"
"html/template"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":4747", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/twttr.js" {
http.ServeFile(w, r, "twttr.js")
return
}
t, _ := template.ParseFiles("home.html", "edit.html")
t.Execute(w, map[string]string{"Title": "My title", "Body": "Hi this is my body"})
}
在我的HTML中,我有类似以下的内容:
<!DOCTYPE html>
<html>
<head>
<title>twitter!</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="twttr.js"></script>
<head>
<body>
<h1>Add new tweet</h1>
<form action="/save/{{.Title}}" method="get">
<button type="button" value="addtweet" class="addtweet">Add Tweet!</button>
</form>
Title is {{.Title}}
{{template "edit.html" .}}
</body>
</html>
JS代码:
$(document).ready(function(){
$('.addtweet').click(function(){
alert("Handler for .click() called.");
});
})
目录结构:
static/
home.html
twttr.js
twttr.go
我认为问题可能与我在GO应用程序中显示模板/HTML的方式有关。但我不知道确切的问题。我对GO和编程都很新,所以非常感谢任何指导!
英文:
I'm trying to load javascript in my GO app, but I got the error:
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:4747/twttr.js". localhost/:6
Uncaught SyntaxError: Unexpected token <
GO:
package main
import (
"net/http"
"html/template"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":4747", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/twttr.js" {
http.ServeFile(w, r, "twttr.js")
return
}
t, _ := template.ParseFiles("home.html", "edit.html")
t.Execute(w, map[string] string {"Title": "My title", "Body": "Hi this is my body"})
}
In my HTML I have something like this:
<!DOCTYPE html>
<html>
<head>
<title>twitter!</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="twttr.js"></script>
<head>
<body>
<h1>Add new tweet</h1>
<form action="/save/{{.Title}}" method="get">
<button type="button" value="addtweet" class="addtweet">Add Tweet!</button>
</form>
Title is {{.Title}}
{{template "edit.html" .}}
</body>
</html>
JS:
$(document).ready(function(){
$('.addtweet').click(function(){
alert("Handler for .click() called.");
});
})
Directory structure:
static/
home.html
twttr.js
twttr.go
I'm thinking it's got something to do with how I display the templates / html in the GO app. But I don't know the exact problem. I'm new to GO and programming in general so any guidance is much appreciated!
答案1
得分: 4
如果你查看JS文件加载的结果,它是否是预期的文件?根据发布的代码,我没有看到你处理它,我猜想可能没有处理。除非你没有发布所有内容。你似乎对每个请求都提供相同的结果,包括JS文件。
要通过GO提供文件,请参考:http://golang.org/pkg/net/http/#ServeFile
你需要检查URL并相应地处理。例如:
if r.URL.Path == "/twttr.js" {
http.ServeFile(w, r, "static/twttr.js")
return
}
t, _ := template.ParseFiles("home.html", "edit.html")
t.Execute(w, map[string]string{"Title": "我的标题", "Body": "嗨,这是我的正文"})
英文:
If you look at your the result of the JS file load, is it even the expected file? I don't see you handling it, based on the posted code, I would guess not. Unless you haven't posted everything. You seem to be serving the same result for every request, including the JS file.
For serving files through GO, look at: http://golang.org/pkg/net/http/#ServeFile
You need to check the URL and handle accordingly. Eg:
if r.URL.Path == "/twttr.js" {
http.ServeFile(w, r, "static/twttr.js")
return
}
t, _ := template.ParseFiles("home.html", "edit.html")
t.Execute(w, map[string] string {"Title": "My title", "Body": "Hi this is my body"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论