0

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 Answer 1

4

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"}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks. I've edited the code according to your suggestion. I may be really off here - I don't fully understand how files are linked to one another in GO yet. Anyway, I ran this on my server and got a 404 page not found message.Appreciate any guidance. thanks!
You probably won't be able to copy paste what I shared, that's just the general idea. You need to look at your file structure and handle accordingly. Go doesn't link files together. Your web browser will request all the files in the HTML, and you need to return the correct one for each request. A 404 is probably a good step. Make sure you JS file actually exists there.
I've updated my directory structure in the code. Hopefully this is enough information - I'm really lost!
Your close, I've updated my example to include "static" when serving the file. That might be all you are missing.
do you think you can explain briefly why I had to add r.URL.Path == "static/twttr.js"? also, what if I want to add css? what do I change in the go app?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.