首页 > 后端开发 > Golang > 正文

Go 代码不打印来自 jquery ajax 的已发布 json 值

WBOY
发布: 2024-02-09 14:30:09
转载
857人浏览过

go 代码不打印来自 jquery ajax 的已发布 json 值

php小编新一分享一种解决方案,帮助你在Go代码中避免打印来自jquery ajax已发布的json值。通过这种方法,你可以有效地控制打印输出,确保代码的可读性和安全性。无论是在前端还是后端开发中,这个技巧都非常实用,帮助你更好地处理json数据。让我们一起来看看具体的实现方法吧!

问题内容

问题详细信息

go 代码未打印来自 jquery ajax 的已发布 json 值

转到代码主

routing := chi.newrouter()
routing.post("/authenticate", authenticaterouter)
登录后复制

go代码

Project IDX
Project IDX

Google推出的一个实验性的AI辅助开发平台

Project IDX 113
查看详情 Project IDX
func authenticaterouter(w http.responsewriter, r *http.request) {
    username := r.postform.get("username")
    fmt.println(r.postformvalue("username"))  //not showing posted value
    fmt.println(r.form.get("username"))       //not showing posted value
    fmt.println(r.form.get("username"))       //not showing posted value
}
登录后复制

jquery ajax 代码

$.ajax({
    "type": "post",
    "url": "authenticate",
    "contenttype": "application/json; charset=utf-8",
    "datatype": "json",
    "data": json.stringify({
        "username": $(form).find("[name='username']").val(),
        "password": $(form).find("[name='password']").val(),
    }),
    beforesend: function() {
    },
    success: function(response) {
        debugger;
    },
    error: function(response) {
        debugger;
    },
    complete: function(response) {
        debugger;
    }
});
登录后复制

html

<form class="loginForm form-signin"><br>    
    <input type="text" name="username" />
    <input type="password" name="password" />
    <button type="submit">Log In</button>
</form>
登录后复制

解决方法

您正在发送 json 数据,但 postform 使用 url 编码数据。你可以这样做:

type authBody struct {
   Username string `json:"username"`
   Password string `json:"password"`
}

func AuthenticateRouter(w http.ResponseWriter, r *http.Request) {
   dec:=json.NewDecoder(r.Body)
   var body authBody
   if err:=dec.Decode(&body); err!=nil {
      // deal with err
   }
   // Work with body.Username and body.Password
}
登录后复制

以上就是Go 代码不打印来自 jquery ajax 的已发布 json 值的详细内容,更多请关注php中文网其它相关文章!

相关标签:
全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号