GopherJS XMLHttpRequest (XHR) and MakeFunc Example
Show how to use MakeFunc in GopherJS by XHR example. This example use MakeFunc to wrap a callback function in XMLHttpRequest (XHR) requests [3].
var readyStateChange = js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
if this.Get("readyState").Int() == 4 {
if this.Get("status").Int() == 200 {
handleGetWordOK(this.Get("responseText").String())
} else {
handleGetWordError()
}
}
return nil
})
func xhrGetWordJson(w string) {
req := js.Global.Get("XMLHttpRequest").New()
req.Call("addEventListener", "readystatechange", readyStateChange)
req.Call("open", "GET", HttpWordJsonPath(w), true)
req.Call("send")
}
Tested on: Ubuntu Linux 15.10, Go 1.5.3.
References:
[1] | GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, ) |
[2] | Bindings · gopherjs/gopherjs Wiki · GitHub |
[3] | print out word explanation · siongui/pali@d140c64 · GitHub |