

XMLHttpRequest (XHR) in Go
source link: http://siongui.github.io/2017/12/14/xmlhttprequest-xhr-in-go/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This post shows how to interact with servers from browsers. Send data to URL or retrieve data from URL from your browser via XMLHttpRequest method in Go/GopherJS. The full code example of this post is on my GitHub.
Retrieve Data From URL
In this example, we will show you how to get data from URL. We will get JSON data from a URL and print the JSON data on the browser console.
JavaScript
var URL = "https://siongui.github.io/xemaauj9k5qn34x88m4h/sacca.json"; function GetJSON() { console.log(this.responseText); } var req = new XMLHttpRequest(); req.addEventListener("load", GetJSON); req.open("GET", URL); req.send();
This example is modified from example on MDN. We get JSON file from the URL and print the data on the console.
GopherJS
The above code in Go/GopherJS is as follows:
package main import ( "github.com/gopherjs/gopherjs/js" ) var URL = "https://siongui.github.io/xemaauj9k5qn34x88m4h/sacca.json" func GetJSON(url string) { req := js.Global.Get("XMLHttpRequest").New() req.Call("addEventListener", "load", func() { println(req.Get("responseText").String()) }) req.Call("open", "GET", url, true) req.Call("send") } func main() { GetJSON(URL) }
Idomatic Go without import GopherJS
Thanks to the great jobs done by GopherJS guys, you can use methods from net/http package in Go standard library to retrieve data from a URL, just like you are writing a local Go program. GopherJS will compile the code and translate the Go code to the corresponding JavaScript XMLHttpRequest for you! You do not even have to know about XMLHttpRequest!
package main import ( "bytes" "net/http" ) var URL = "https://siongui.github.io/xemaauj9k5qn34x88m4h/sacca.json" func GetJSON(url string) { resp, err := http.Get(url) if err != nil { return } defer resp.Body.Close() if resp.StatusCode != 200 { return } buf := new(bytes.Buffer) buf.ReadFrom(resp.Body) println(buf.String()) } func main() { GetJSON(URL) }
Run Code on GopherJS Playground
The result of above code is the same as the Go code in previous section. Just compile the code with GopherJS, and the JavaScript code output from GopherJS will run with the same result! Amazing!
References:
[1]GopherJS XMLHttpRequest (XHR) and MakeFunc Example
[2][Golang] XMLHttpRequest (XHR) HTTP POST JSON Data by GopherJS
[3][Golang] XMLHttpRequest (XHR) HTTP GET JSON Data by GopherJS
[4][Golang] Access HTTP Request Header by XHR getAllResponseHeaders()
Recommend
-
97
RESEARCH: Posting a file with XMLHttpRequest Time-stamp: <2018-11-06 14:26:38 tamara> published date: [2018-11-06 Tue 13:46] keywords: XMLHttpRequest, file upload, JavaScrip...
-
20
啥都不说了,挺华为就完事了,山姆大叔有点过分了,我相信华为能挺过来 现在该我了! 啊,贾克斯???现在从事前端的小伙伴不可能不知道这个,如果真不知道这个词,那我觉得你还称不上前端开发? 此贾克斯非彼贾克斯,前端说的啊贾克斯是Asynchronous jav
-
25
一、XMLHttpRequest的发展历程 Ajax技术的核心是XMLHttpRequest对象。我们使用XMLHttpRequest对象来发送一个Ajax请求。这是由微软首先引入的一个特性,其他浏览器提供商后来都提供了相同的实现。...
-
10
Encapsulating Ajax XMLHTTPRequest Calls within JavaScript classes Tuesday, January 31, 2006 The guys over at Fold [posted][1] an article on how to use multiple XMLHTTPRequest (XHR) calls within a single page. This is somethi...
-
7
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
6
[Go WebAssembly] XMLHttpRequest (XHR) October 19, 2018 This post shows how to...
-
11
[Vue.js] AJAX XMLHttpRequest (XHR) Example March 22, 2017 Examp...
-
7
Introduction Access HTTP Request Header in Golang code running on your browser. This can be done by
-
6
GopherJS XMLHttpRequest (XHR) and MakeFunc Example February 18, 2016...
-
16
Introduction It is really cool to run Go code in the browser. GopherJS is a compiler from Go to
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK