diff --git a/handlers.go b/handlers.go index 5c7a326..6ab46f8 100644 --- a/handlers.go +++ b/handlers.go @@ -14,6 +14,13 @@ type prob struct { Last int } +type JsonHandler func(http.ResponseWriter, *http.Request) + +func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + w.Header().Set("Content-Type", "application/json") + h(w, req) +} + func problem(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { diff --git a/main.go b/main.go index 15aaa09..4e1f35e 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,8 @@ func main() { flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files))) - http.HandleFunc("/api/v0/attempt/", attempt) - http.HandleFunc("/api/v0/problem/", problem) + http.HandleFunc("/api/v0/attempt/", JsonHandler(attempt)) + http.Handle("/api/v0/problem/", JsonHandler(problem)) if err := http.ListenAndServe(*addr, nil); err != nil { log.Fatal("ListenAndServe:", err) } diff --git a/static/math.js b/static/math.js index 1cfb180..7600e6d 100644 --- a/static/math.js +++ b/static/math.js @@ -7,7 +7,7 @@ function update_board(f, o, s, score) { function new_problem() { $.get("/api/v0/problem/", function(d) { - update_board(d["first"], d["operation"], d["second"], d["score"]); + update_board(d["First"], d["Operation"], d["Second"], d["Score"]); $("#answer").val(""); }) }