GO: JavaScript和HTML MIME错误

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

GO: javascript and html MIME error

问题

我正在尝试在我的GO应用程序中加载JavaScript,但是我遇到了错误:

  1. Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:4747/twttr.js". localhost/:6
  2. Uncaught SyntaxError: Unexpected token <

GO代码:

  1. package main
  2. import (
  3. "net/http"
  4. "html/template"
  5. )
  6. func main() {
  7. http.HandleFunc("/", handler)
  8. http.ListenAndServe(":4747", nil)
  9. }
  10. func handler(w http.ResponseWriter, r *http.Request) {
  11. if r.URL.Path == "/twttr.js" {
  12. http.ServeFile(w, r, "twttr.js")
  13. return
  14. }
  15. t, _ := template.ParseFiles("home.html", "edit.html")
  16. t.Execute(w, map[string]string{"Title": "My title", "Body": "Hi this is my body"})
  17. }

在我的HTML中,我有类似以下的内容:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>twitter!</title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  6. <script type="text/javascript" src="twttr.js"></script>
  7. <head>
  8. <body>
  9. <h1>Add new tweet</h1>
  10. <form action="/save/{{.Title}}" method="get">
  11. <button type="button" value="addtweet" class="addtweet">Add Tweet!</button>
  12. </form>
  13. Title is {{.Title}}
  14. {{template "edit.html" .}}
  15. </body>
  16. </html>

JS代码:

  1. $(document).ready(function(){
  2. $('.addtweet').click(function(){
  3. alert("Handler for .click() called.");
  4. });
  5. })

目录结构:

  1. static/
  2. home.html
  3. twttr.js
  4. twttr.go

我认为问题可能与我在GO应用程序中显示模板/HTML的方式有关。但我不知道确切的问题。我对GO和编程都很新,所以非常感谢任何指导!

英文:

I'm trying to load javascript in my GO app, but I got the error:

  1. Resource interpreted as Script but transferred with MIME type text/html: &quot;http://localhost:4747/twttr.js&quot;. localhost/:6
  2. Uncaught SyntaxError: Unexpected token &lt;

GO:

  1. package main
  2. import (
  3. &quot;net/http&quot;
  4. &quot;html/template&quot;
  5. )
  6. func main() {
  7. http.HandleFunc(&quot;/&quot;, handler)
  8. http.ListenAndServe(&quot;:4747&quot;, nil)
  9. }
  10. func handler(w http.ResponseWriter, r *http.Request) {
  11. if r.URL.Path == &quot;/twttr.js&quot; {
  12. http.ServeFile(w, r, &quot;twttr.js&quot;)
  13. return
  14. }
  15. t, _ := template.ParseFiles(&quot;home.html&quot;, &quot;edit.html&quot;)
  16. t.Execute(w, map[string] string {&quot;Title&quot;: &quot;My title&quot;, &quot;Body&quot;: &quot;Hi this is my body&quot;})
  17. }

In my HTML I have something like this:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;twitter!&lt;/title&gt;
  5. &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;
  6. &lt;script type=&quot;text/javascript&quot; src=&quot;twttr.js&quot;&gt;&lt;/script&gt;
  7. &lt;head&gt;
  8. &lt;body&gt;
  9. &lt;h1&gt;Add new tweet&lt;/h1&gt;
  10. &lt;form action=&quot;/save/{{.Title}}&quot; method=&quot;get&quot;&gt;
  11. &lt;button type=&quot;button&quot; value=&quot;addtweet&quot; class=&quot;addtweet&quot;&gt;Add Tweet!&lt;/button&gt;
  12. &lt;/form&gt;
  13. Title is {{.Title}}
  14. {{template &quot;edit.html&quot; .}}
  15. &lt;/body&gt;
  16. &lt;/html&gt;

JS:

  1. $(document).ready(function(){
  2. $(&#39;.addtweet&#39;).click(function(){
  3. alert(&quot;Handler for .click() called.&quot;);
  4. });
  5. })

Directory structure:

  1. static/
  2. home.html
  3. twttr.js
  4. 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并相应地处理。例如:

  1. if r.URL.Path == "/twttr.js" {
  2. http.ServeFile(w, r, "static/twttr.js")
  3. return
  4. }
  5. t, _ := template.ParseFiles("home.html", "edit.html")
  6. 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:

  1. if r.URL.Path == &quot;/twttr.js&quot; {
  2. http.ServeFile(w, r, &quot;static/twttr.js&quot;)
  3. return
  4. }
  5. t, _ := template.ParseFiles(&quot;home.html&quot;, &quot;edit.html&quot;)
  6. t.Execute(w, map[string] string {&quot;Title&quot;: &quot;My title&quot;, &quot;Body&quot;: &quot;Hi this is my body&quot;}

huangapple
  • 本文由 发表于 2014年7月28日 00:21:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/24982916.html
匿名

发表评论

匿名网友

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

确定