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!