Golang how does the background of writing a web project return json data to the front end instead of displaying it directly on the page?

the separation of front and rear ends is not mentioned in the go web programming book, and beginners are confused in learning. I use fmt.Fprint to write json data to the page. How to call the interface at the front end, I return the json data instead of writing it directly on the page

Apr.05,2021

func handler(w http.ResponseWriter, r *http.Request){
    var body json.RawMessage
    // todo
    
    b, _ := body.MarshalJSON()
    w.Header().Set("Content-Length", strconv.Itoa(len(b)))
    w.Write(b)
}

do your processing in todo


api, in http mode can be seen in the browser, and the front-end page can get the value in ajax mode.

Menu