

[Go WebAssembly] XMLHttpRequest (XHR)
source link: http://siongui.github.io/2018/10/19/golang-wasm-xmlhttprequest-xhr/
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.

[Go WebAssembly] XMLHttpRequest (XHR)
October 19, 2018
This post shows how to interact with servers from browsers. Retrieve data from URL from your browser via Go WebAssembly. See demo first:
Go Wasm XMLHttpRequest (XHR) Demo
Using XMLHttpRequest is the most common way to issue HTTP requests for data exchange between browsers and servers. In Go Wasm, we do not need to deal with XMLHttpRequest at all for data exchange. We can write idomatic Go code and use net/http package in Go standard library to issue HTTP requests. You do not even have to know about XMLHttpRequest!
The following is the source code of the demo:
Go:
package main import ( "bytes" "fmt" "net/http" ) func GetJson(url string) (json string, err error) { resp, err := http.Get(url) if err != nil { return } defer resp.Body.Close() if resp.StatusCode != 200 { err = fmt.Errorf("response status code: %d", resp.StatusCode) return } var buf bytes.Buffer _, err = buf.ReadFrom(resp.Body) if err != nil { return } json = buf.String() return } func main() { json, err := GetJson("https://siongui.github.io/xemaauj9k5qn34x88m4h/sacca.json") if err != nil { panic(err) } fmt.Println(json) }
HTML:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Go wasm - XMLHttpRequest (XHR)</title> </head> <body> Please open developer console to see the retrieved JSON data from server. <!-- https://github.com/golang/go/blob/master/misc/wasm/wasm_exec.js --> <script src="wasm_exec.js"></script> <script> const go = new Go(); let mod, inst; WebAssembly.instantiateStreaming( fetch("xhr.wasm", {cache: 'no-cache'}), go.importObject).then((result) => { mod = result.module; inst = result.instance; run(); }); async function run() { await go.run(inst); }; </script> </body> </html>
Nothing special in above HTML code. Most of the HTML code is to load compiled wasm module. If you have no idea what it means, see [1].
The full source code is also available in my GitHub repo.
Tested on:
- Ubuntu Linux 18.04
- Go 1.11.1
- Chromium Version 69.0.3497.81 on Ubuntu 18.04 (64-bit)
References:
[2]XMLHttpRequest (XHR) in Go (GopherJS)
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...
-
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...
-
21
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 exam...
-
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