How to get and AJAX response using Golang with Gin Routing

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

How to get and AJAX response using Golang with Gin Routing

问题

这是我的 Golang 代码(注意在 POST ("/register") 处理程序中的 registerNewUser):

  1. func main() {
  2. router := gin.Default()
  3. router.LoadHTMLFiles("../login.html", "../recoverPassword.html")
  4. router.Static("/css", "../css")
  5. router.Static("/img", "../img")
  6. router.Static("/js", "../js")
  7. router.GET("/", loadMainPage)
  8. router.POST("/register", registerNewUser)
  9. router.Run("localhost:8080")
  10. }
  11. func registerNewUser(c *gin.Context) {
  12. fmt.Print("Nonas")
  13. log.Print("Sisas")
  14. }

现在,这是我的 JavaScript 代码:

  1. $.ajax({
  2. url: "/register",
  3. method: "POST",
  4. data: {firstname:Firstname, lastname:Lastname, username:Username, email:Email, password:Password, country:Country},
  5. beforeSend:function(){
  6. $("#submit_register_form_btn").attr('disabled', true);
  7. $("#submit_register_form_btn").val('Conectando...');
  8. },
  9. success:function(response) {
  10. $('#submit_register_form_btn').removeAttr('disabled');
  11. alert(response);
  12. }
  13. });

我需要一种方法将字符串作为响应从 Golang 返回给 AJAX 请求。

英文:

Here is my code of Golang (Notice the registerNewUser in the POST ("/register") handler:

  1. func main(){
  2. router := gin.Default()
  3. router.LoadHTMLFiles("../login.html", "../recoverPassword.html")
  4. router.Static("/css", "../css")
  5. router.Static("/img", "../img")
  6. router.Static("/js", "../js")
  7. router.GET("/", loadMainPage)
  8. router.POST("/register", registerNewUser)
  9. router.Run("localhost:8080")
  10. }
  11. func registerNewUser(c *gin.Context) {
  12. fmt.Print("Nonas")
  13. log.Print("Sisas")
  14. }

Now, here is my code of javascript:

  1. $.ajax({
  2. url: "/register",
  3. method: "POST",
  4. data: {firstname:Firstname, lastname:Lastname, username:Username, email:Email, password:Password, country:Country},
  5. beforeSend:function(){
  6. $("#submit_register_form_btn").attr('disabled', true);
  7. $("#submit_register_form_btn").val('Conectando...');
  8. },
  9. success:function(response) {
  10. $('#submit_register_form_btn').removeAttr('disabled');
  11. alert(response);
  12. }
  13. });

I need a way to return a string as response from Golang to the AJAX petition

答案1

得分: 1

fmtlog分别打印到os.Stdoutos.Stderr。你需要对web请求做出响应。

Gin将一个*gin.Context传递给你的处理程序来实现这个目的。

https://pkg.go.dev/github.com/gin-gonic/gin#readme-quick-start展示了一个非常简单的通过上下文返回数据的示例。在https://pkg.go.dev/github.com/gin-gonic/gin#readme-api-examples中还有很多其他很棒的示例。

在查看了一些示例以理解通常的用法后,“上下文是gin最重要的部分”,所以请仔细阅读文档

一旦你觉得对快速入门有所了解,可以看一下gin的Context.String。你可以将其替换为示例中的c.JSON调用。

英文:

fmt and log print to os.Stdout and os.Stderr respectively. You need to respond to the web request.

Gin passes a *gin.Context to your handler for this purpose.

https://pkg.go.dev/github.com/gin-gonic/gin#readme-quick-start shows a very small example of returning data through the context. There are tons of other great examples in https://pkg.go.dev/github.com/gin-gonic/gin#readme-api-examples.

"Context is the most important part of gin," so read the documentation thoroughly after reviewing a few examples to wrap your head around what usage generally looks like.

Once you feel like you have your head wrapped around the quickstart, take a look at gin's Context.String . You can substitute that for the c.JSON call in the example.

huangapple
  • 本文由 发表于 2021年10月17日 04:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/69599289.html
匿名

发表评论

匿名网友

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

确定