How to get and AJAX response using Golang with Gin Routing

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

How to get and AJAX response using Golang with Gin Routing

问题

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

func main() {
    router := gin.Default()
    router.LoadHTMLFiles("../login.html", "../recoverPassword.html")
    router.Static("/css", "../css")
    router.Static("/img", "../img")
    router.Static("/js", "../js")
    router.GET("/", loadMainPage)
    router.POST("/register", registerNewUser)

    router.Run("localhost:8080")
}

func registerNewUser(c *gin.Context) {
    fmt.Print("Nonas")
    log.Print("Sisas")
}

现在,这是我的 JavaScript 代码:

$.ajax({
    url: "/register",
    method: "POST",
    data: {firstname:Firstname, lastname:Lastname, username:Username, email:Email, password:Password, country:Country},
    beforeSend:function(){
        $("#submit_register_form_btn").attr('disabled', true);
        $("#submit_register_form_btn").val('Conectando...');
    },
    success:function(response) {
        $('#submit_register_form_btn').removeAttr('disabled');
        alert(response);
    }
});

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

英文:

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

func main(){

	router := gin.Default()
	router.LoadHTMLFiles("../login.html", "../recoverPassword.html")
	router.Static("/css", "../css")
	router.Static("/img", "../img")
	router.Static("/js", "../js")
	router.GET("/", loadMainPage)
	router.POST("/register", registerNewUser)

	router.Run("localhost:8080")
}
func registerNewUser(c *gin.Context) {
	fmt.Print("Nonas")
	log.Print("Sisas")
}

Now, here is my code of javascript:

$.ajax({
                        url: "/register",
                        method: "POST",
                        data: {firstname:Firstname, lastname:Lastname, username:Username, email:Email, password:Password, country:Country},
                        beforeSend:function(){
                            $("#submit_register_form_btn").attr('disabled', true);
                            $("#submit_register_form_btn").val('Conectando...');
                        },
                        success:function(response) {
                            $('#submit_register_form_btn').removeAttr('disabled');
                            alert(response);
                        }
                    });

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:

确定