Jquery ajax将如何从golang代码中获取数据?
我有一个用于保存和检索表单数据的GolangAPI。保存HTML表单中填写的数据是可以的。这意味着它将成功地将数据保存在mongodb的数据库中,但在检索数据的情况下,它将根据html表单字段中填写的电子邮件检索数据,这是成功的,但它会在ubuntu的终端中显示数据。下面是我为此尝试的代码:- Html表单index.Html文件Jquery ajax将如何从golang代码中获取数据?,jquery,html,ajax,go,Jquery,Html,Ajax,Go,我有一个用于保存和检索表单数据的GolangAPI。保存HTML表单中填写的数据是可以的。这意味着它将成功地将数据保存在mongodb的数据库中,但在检索数据的情况下,它将根据html表单字段中填写的电子邮件检索数据,这是成功的,但它会在ubuntu的终端中显示数据。下面是我为此尝试的代码:- Html表单index.Html文件 <form id="form1" method="post"> <input id= "firstname" type="text" nam
<form id="form1" method="post">
<input id= "firstname" type="text" name="FirstName" placeholder="Enter your firstname"><br><br>
<input id= "lastname" type="text" name="LastName" placeholder="Enter your lastname"><br><br>
<input id= "email" type="text" name="Email" placeholder="Enter your email"><br><br>
<input id= "mobile" type="text" name="Mobile" placeholder="Enter your mobile"><br><br>
<button id= "button1" class="button" name="button" type="button" value="Submit">Submit</button>
<button id= "button2" class="button" name="button" type="submit" value="Search">Search</button>
</form>
Main.go文件
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"html/template"
"log"
"net/http"
"encoding/json"
)
type USER struct {
FirstName string `json:"FirstName,omitempty"`
LastName string `json:"LastName,omitempty"`
Email string `json:"Email,omitempty"`
Mobile string `json:"Mobile,omitempty"`
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
} else {
r.ParseForm()
fmt.Println(r.Form)
if r.Form["button"][0] == "Submit" {
fmt.Println(r.Form)
session, err := mgo.Dial("mongodb://127.0.0.1:27017/user")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("user").C("profile")
doc := USER{
FirstName: r.Form["first"][0],
LastName: r.Form["last"][0],
Email: r.Form["email"][0],
Mobile: r.Form["mobile"][0],
}
err = c.Insert(doc)
if err != nil {
panic(err)
}
fmt.Println("FistName:", r.Form["first"][0])
fmt.Println("LastName:", r.Form["last"][0])
fmt.Println("Email:", r.Form["email"][0])
fmt.Println("Mobile:", r.Form["mobile"][0])
}
}
}
func AllData(w http.ResponseWriter, r *http.Request){
fmt.Println("method:", r.Method)
if r.Method == "GET" {
r.ParseForm()
fmt.Println(r.Form)
if r.Form["button"][0] == "Search" {
fmt.Println(r.Form)
fmt.Println(r.Form["email"][0])
session, err := mgo.Dial("mongodb://127.0.0.1:27017/user")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("user").C("profile")
result := Users{}
err = c.Find(bson.M{"email": r.Form["email"][0]}).All(&result)
fmt.Println(result)
b, err := json.MarshalIndent(result, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b)
}
}
}
func main() {
http.HandleFunc("/login", login)
http.HandleFunc("/get-data", AllData)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
我应该做什么来接收从golang到ajax的数据
GET
方法请求单击按钮搜索,结果将显示在ajax的成功函数中。先谢谢你 以json格式发送数据
函数的响应头,并编写json响应,该响应可在$成功接收。ajax
func AllData(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
// your code ....
b, err := json.MarshalIndent(result, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b)
// set header to 'application/json'
w.Header().Set("Content-Type", "application/json")
// write the response
w.Write(b)
}
}
}
另外,从handler func
AllData
返回的代码中也有一个错误,如果您想对结果进行一些修改,请创建一个中间件,或者删除handlerAllData的返回部分,这将给我一个错误无法使用字符串(b)(类型字符串)作为类型[]w.Write./main.go:98:1:函数末尾缺少返回值。/main.go:103:18:无法将AllData(type func(http.ResponseWriter,*http.Request)用户)用作http.HandleFunc参数中的type func(http.ResponseWriter,*http.Request)用户(http.ResponseWriter,*http.Request)用户)作为func类型(http.ResponseWriter,*http.Request)在http.HandleFunc的参数中
是,因为您的所有数据
正在返回用户
,这是一个错误。请检查编辑的代码。是的,您可以。但是我已经迟到了一些时间,您可以删除您的查询。我会检查它为什么会给出我告诉您的错误?您给出的代码可以,但您能解释一下吗对我来说,它到底在做什么?
func AllData(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
// your code ....
b, err := json.MarshalIndent(result, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b)
// set header to 'application/json'
w.Header().Set("Content-Type", "application/json")
// write the response
w.Write(b)
}
}
}