如何使用HTML / javascript / jquery将golang http json body输出到网页? [英] How to output golang http json body to website page using HTML/javascript/jquery?
问题描述
我有一个Golang网站,我希望使用SQLite的Mobile App Quickstart的名为SwaggerUI的API在我的UWP游戏中显示得分。我通过执行HTTP GET请求获得分数。问题是以JSON格式输出到Golang控制台的分数。我想在实际网站上显示分数。为了做到这一点,我怎么能在前端调用我的golang函数?前端是用HTML / Javascript / JQuery编写的。
这是我的Golang代码:
< br $>
I've got a Golang Website where I want to display 'scores' from my UWP Game using SQLite's Mobile App Quickstart's API called SwaggerUI. I am getting the scores by doing a HTTP GET request. The problem is that the scores output to the Golang console in JSON Format. I want to display the scores onto the actual website. How could I call my golang function in the Frontend in order to do this? The frontend is written in HTML/Javascript/JQuery.
This is my Golang code:
func scoresPage(res http.ResponseWriter, req *http.Request) {
//Connecting to SwaggerUI API to get Scores from Azure for UWP Application
req, err := http.NewRequest("GET", os.ExpandEnv("https://brainworksappservice.azurewebsites.net/tables/TodoItem?$select=score"), nil)
if err != nil {
log.Fatal(err)
}
//You have to specify these headers
req.Header.Set("Accept", "application/json")
//If you do not specify what version your API is, you cannot receive the JSON
req.Header.Set("Zumo-Api-Version", "2.0.0")
//Do the request
resp, err := http.DefaultClient.Do(req)
//Error if the request cannot be done
if err != nil {
log.Fatal(err)
}
//You need to close the Body everytime, as if you don't you could leak information
defer resp.Body.Close()
//Read all of the information from the body
body, err := ioutil.ReadAll(resp.Body)
//Error if the info cannot be read
if err != nil {
log.Fatal(err)
}
//Write the JSON to the standard output (the Console)
_, err = os.Stdout.Write(body)
//Error if the info cannot be output to the console
if err != nil {
log.Fatal(err)
}
http.ServeFile(res, req, "Scores.html")
}
func main() { http.HandleFunc("/scores", scoresPage)
//serve on the port 8000 forever
http.ListenAndServe(":8000", nil)
}
我的尝试:
我我尝试过使用各种Javascript方法,但到目前为止还没有。
What I have tried:
I've tried using various Javascript methods but none have worked so far.
推荐答案
select = score),nil)
if err!= nil {
log.Fatal(err)
}
//你必须指定这些头文件
req.Header.Set(Accept,application / json )
//如果您没有指定API的版本,则无法收到JSON
req.Header.Set(Zumo-Api-Version,2.0.0)
//请求
resp,错误:= http.DefaultClient.Do(req)
//如果请求无法完成则出错
if err!= nil {
log.Fatal(错误)
}
//你需要每次都关闭Body,好像你不能泄漏信息
defer resp。 Body.Close()
//读取正文中所有信息
body,err:= ioutil.ReadAll(resp.Body)
//如果无法读取信息则出错
if err!= nil {
log.Fatal(err)
}
//将JSON写入标准输出(控制台)
_,错误= os.Stdout.Write(正文)
//如果信息无法输出到控制台则出错
if err!= nil {
log .Fatal(错误)
}
http.ServeFile(res,req,Scores.html)
}
func main(){ http.HandleFunc(/ scores,scoresPage)
//永远在端口8000上提供服务
http.ListenAndServe(:8000,零)
}
select=score"), nil) if err != nil { log.Fatal(err) } //You have to specify these headers req.Header.Set("Accept", "application/json") //If you do not specify what version your API is, you cannot receive the JSON req.Header.Set("Zumo-Api-Version", "2.0.0") //Do the request resp, err := http.DefaultClient.Do(req) //Error if the request cannot be done if err != nil { log.Fatal(err) } //You need to close the Body everytime, as if you don't you could leak information defer resp.Body.Close() //Read all of the information from the body body, err := ioutil.ReadAll(resp.Body) //Error if the info cannot be read if err != nil { log.Fatal(err) } //Write the JSON to the standard output (the Console) _, err = os.Stdout.Write(body) //Error if the info cannot be output to the console if err != nil { log.Fatal(err) } http.ServeFile(res, req, "Scores.html") } func main() { http.HandleFunc("/scores", scoresPage) //serve on the port 8000 forever http.ListenAndServe(":8000", nil) }
我试过的:
我尝试过使用各种Javascript方法,但到目前为止还没有。
What I have tried:
I've tried using various Javascript methods but none have worked so far.
这篇关于如何使用HTML / javascript / jquery将golang http json body输出到网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!