GO: JavaScript和HTML MIME错误

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

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:     &quot;http://localhost:4747/twttr.js&quot;. localhost/:6
Uncaught SyntaxError: Unexpected token &lt; 

GO:

package main

import (
    &quot;net/http&quot;
    &quot;html/template&quot;
)

func main() {
    http.HandleFunc(&quot;/&quot;, handler)
    http.ListenAndServe(&quot;:4747&quot;, nil) 
}

func handler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == &quot;/twttr.js&quot; {
        http.ServeFile(w, r, &quot;twttr.js&quot;)
        return
    } 
    t, _ := template.ParseFiles(&quot;home.html&quot;, &quot;edit.html&quot;)
    t.Execute(w, map[string] string {&quot;Title&quot;: &quot;My title&quot;, &quot;Body&quot;: &quot;Hi this is my body&quot;})
}

In my HTML I have something like this:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;twitter!&lt;/title&gt;
    &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js&quot;&gt;&lt;/script&gt;
	    &lt;script type=&quot;text/javascript&quot; src=&quot;twttr.js&quot;&gt;&lt;/script&gt;
&lt;head&gt;

&lt;body&gt;
    &lt;h1&gt;Add new tweet&lt;/h1&gt;
    &lt;form action=&quot;/save/{{.Title}}&quot; method=&quot;get&quot;&gt;
    &lt;button type=&quot;button&quot; value=&quot;addtweet&quot; class=&quot;addtweet&quot;&gt;Add Tweet!&lt;/button&gt;
    &lt;/form&gt;
    Title is {{.Title}}
    {{template &quot;edit.html&quot; .}}
&lt;/body&gt;
&lt;/html&gt;

JS:

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

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 == &quot;/twttr.js&quot; {
    http.ServeFile(w, r, &quot;static/twttr.js&quot;)
    return
} 

t, _ := template.ParseFiles(&quot;home.html&quot;, &quot;edit.html&quot;)
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:

确定