javascript - 如何使用jQuery从golang代码(服务器)获取数据?

标签 javascript go

用户将使用网站上提供的文本编辑器提交数据。我的golang服务器将捕获它,对其进行处理,然后在网站上的文本编辑器下方显示结果。

我可以获取用户输入并进行处理,但是无法将其发送回网站以显示结果。

主功能:

    http.Handle("/", http.FileServer(http.Dir("./codemirror")))
    http.HandleFunc("/getRules", getRules)
...

getRules函数:
//capture form data
//do some processing on captured data
    js, _ := json.Marshal(resp)
    w.Header().Set("Content-Type", "application/json")
    w.Write(js)

index.html:
<!DOCTYPE html>
<html>
    <head>
        <title>nvrule playground</title>
        <link rel="stylesheet" type="text/css" href="plugin/codemirror/lib/codemirror.css">
    <link rel="stylesheet" type="text/css" href="plugin/codemirror/theme/the-matrix.css">
        <style>
        .button {
          padding: 15px 25px;
          font-size: 24px;
          text-align: center;
          cursor: pointer;
          outline: none;
          color: #fff;
          background-color: #4CAF50;
          border: none;
          border-radius: 15px;
          box-shadow: 0 9px #999;
        }

        .button:hover {background-color: #3e8e41}

        .button:active {
          background-color: #3e8e41;
          box-shadow: 0 5px #666;
          transform: translateY(4px);
        }
        </style>
    </head>
    <body>
        <iframe name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>

        <form action="/getRules" method="post" id="ruleForm" target="hiddenFrame">
            <textarea id="codemirror-textarea" name="rule"></textarea>
            <input class="button" type="submit" value="Send" />
        </form>

        <!-- javascript -->
        <script type="text/javascript" src="plugin/codemirror/lib/codemirror.js"></script>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/default.js"></script>
    </body>
</html>

default.js:
var editor = CodeMirror.fromTextArea(document.getElementById("codemirror-textarea"), {
    lineNumbers : true,
    theme : "the-matrix",
    value: "Enter rules here"
  });

最佳答案

我想到了。您可以使用ajax。

$('#ruleForm').submit(function(e){
    e.preventDefault(); // avoid to execute the actual submit of the form.
    var form = $(this);
    var url = form.attr('action');
        $.ajax({
                   type: "POST",
                   url: url,
                   data: form.serialize(),
                   success: function(json){
                   console.log(json)
                   var templateHtml = $('#resultsArea').html(),
                   template = Handlebars.compile(templateHtml);
                   $('#ajaxResponse').html(template(json));
            }
        });

    });

有用的链接:stackoverflow (question contains my answer)

关于javascript - 如何使用jQuery从golang代码(服务器)获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61994208/

相关文章:

go - 带有 TB RAM 的 go 1.5 gc 有多快?

javascript - Angularjs:无法解析带单引号的 JSON

Go type 在它看起来不应该的时候自动转换

javascript - 为什么我得到的是 jQuery 对象的子属性值而不是父属性值?

javascript - 在 Backbone 中删除嵌套 View 的有效方法

go - 隐式转换在应该成功的时候失败了?

go - 不为 dll 或 iso 文件添加扩展名是一种不好的做法吗?

for-loop - 如何在 Golang 中迭代结构中的 slice 或者是否可以在结构中使用 for 循环?

javascript - Gulp 没有覆盖目标文件

c# - 如何在asp.net中打印标签或面板的内容?