Golang Gin处理AJAX请求

时间:2017-09-21 11:35:44

标签: go

从前端部分发送ajax请求到golang服务器

ajax请求示例:

    var sendAjax = function (method, url, body) {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === XMLHttpRequest.DONE ) {
            if (xmlhttp.status === 200) {
                console.log('success');
            } else if (xmlhttp.status === 400) {
                alert('There was an error 400');
            } else {
                alert('something else other than 200 was returned');
            }
        }
    };

    xmlhttp.open(method, url, true);
    xmlhttp.send(JSON.stringify({'b44': 'sdfsdfsdfs}));
};

Golang方

    router.POST("/save", func(context *gin.Context) {
    if err := context.Request.ParseForm(); err != nil {
        fmt.Println("Cannot ParseForm. Error:", err)
    }

    var b64FromRequest string = context.Request.FormValue("b44") //null here :(

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

而不是context.Request.FormValue("b44")尝试context.PostForm("b44")