如何在GAE GO中实现AJAX

huangapple go评论90阅读模式
英文:

How to implement AJAX in GAE GO

问题

我知道可以使用jQuery Ajax进行ajax post,但是调用会返回一个值,文本,html还是json?谢谢!

英文:

I know it is possible to do an ajax post using jQuery Ajax but how does the call would return a value, text, html or json? Thanks!

答案1

得分: 9

这里有一个非常好的关于在Go中创建Web应用程序的入门指南:http://golang.org/doc/articles/wiki/

处理POST请求的示例在saveHandler函数中给出:

func saveHandler(w http.ResponseWriter, r *http.Request) {
    title := r.URL.Path[lenPath:]
    body := r.FormValue("body")
    p := &Page{Title: title, Body: []byte(body)}
    p.save()
    http.Redirect(w, r, "/view/"+title, http.StatusFound)
}

在你的情况下,不要重定向,只需返回一个字符串,例如:

func saveHandler(w http.ResponseWriter, r *http.Request) {
    value := r.FormValue("inputVal")
    err := saveFunction(value)
    if err == nil {
        fmt.Fprintf(w, "OK")
    } else {
       fmt.Fprintf(w, "NG")
    }
}

func main() {
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/save", saveHandler)
    http.ListenAndServe(":8080", nil)
}

..然后(因为你使用了jQuery),使用回调函数处理它,如http://api.jquery.com/jQuery.post/中所示:

$.post('/save', {inputVal: "banana"}, function(data) {
  if(data == "OK") {
    alert("Saved!");
  } else {
    alert("Save Failed!");
  }
});

如果你想返回JSON,你需要学习如何编组你的数据,然后像上面返回字符串一样返回它。

这里是关于JSON文档的链接:http://golang.org/pkg/encoding/json/#Marshal

熟悉如何使用它的一个好方法是在http://play.golang.org上进行一些实验,编组和解组结构并将其打印出来。这是一个示例:http://play.golang.org/p/OHVEGzD8KW

英文:

There's a very good primer on creating web applications in Go here: http://golang.org/doc/articles/wiki/

The example for handling a POST is given in the saveHandler function:

func saveHandler(w http.ResponseWriter, r *http.Request) {
    title := r.URL.Path[lenPath:]
    body := r.FormValue("body")
    p := &Page{Title: title, Body: []byte(body)}
    p.save()
    http.Redirect(w, r, "/view/"+title, http.StatusFound)
}

In your case, instead of redirecting, just return a string eg.

func saveHandler(w http.ResponseWriter, r *http.Request) {
    value := r.FormValue("inputVal")
    err := saveFunction(value)
    if err == nil {
        fmt.Fprintf(w, "OK")
    } else {
       fmt.Fprintf(w, "NG")
    }
}

func main() {
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/save", saveHandler)
    http.ListenAndServe(":8080", nil)
}

.. and (since you're using jQuery), handle it with a callback as shown in http://api.jquery.com/jQuery.post/:

$.post('/save', {inputVal: "banana"}, function(data) {
  if(data == "OK") {
    alert("Saved!");
  } else {
    alert("Save Failed!");
  }
});

If you want to return JSON, you'll need to learn how to Marshal your data, then return that like we return the string above.

Here is a link to to the JSON documentation: http://golang.org/pkg/encoding/json/#Marshal

A good way to get familiar with how to use it is to have a play around on http://play.golang.org, marshalling and unmarshalling structs and printing them out. Here's an example: http://play.golang.org/p/OHVEGzD8KW

huangapple
  • 本文由 发表于 2013年1月17日 09:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/14370616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定