dqpdb82600 2015-03-30 18:27
浏览 1230
已采纳

从javascript代码中调用Golang函数

I'm working on a webapp which already has a layout css, bootstrap v.3 along with an index.html. I have successfully loaded the project with Golang up and running. I have embedded a signup button which upon click is supposed to call a Go function from within the server.go file that handles http requests.

    $(document).ready(function() {
    $('#signup').on('click', loginHandler);
});

I have a server.go file written like this:

package main

import (
    "net/http"

    "github.com/bmizerany/pat"
)

func init() {
    m := pat.New()

    m.Get("/signup", http.HandlerFunc(loginHandler))
    m.Get("/", http.HandlerFunc(rootHandler))
    http.Handle("/", m)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, r.URL.Path[1:])
}

func loginHandler(w http.ResponseWriter, r *http.Request) {

}

So the question is upon click on an button instance with signup Id, how do I have to trigger the golang loginHandler function in server.go file? Any idea on this would be appreciated.

  • 写回答

1条回答 默认 最新

  • dongling4288 2015-03-30 18:31
    关注

    What you are looking for is called AJAX (Asynchronous Javascript And Xml). It is a JavaScript technology that allows you make asynchronous HTTP requests to get data from the servers. It seems that you are using jQuery, and using jQuery with AJAX, would look like this:

    $.ajax({
      url: "http://www.example.com/signup",
      data: {username: "whatever"} //If the request needs any data 
    }).done(function (data) {
      // Do whatever with returned data
    });
    

    if you want, you can use functions specifically for GET and POST:

    $.get("url: "http://www.example.com/signup", function (data) {
      // Do whatever with the returned data
    });
    
    $.post("url: "http://www.example.com/signup", {username: "whatever"}, function (data) {
      // Do whatever with the returned data
    });
    

    AJAX can even be performed without jQuery:

    var req = new XMLHTTPRequest();
    req.addEventListener("load", function (data) {// Do whatever});
    req.open("get", "http://example.com", true);
    req.send();
    

    If you need a reference for AJAX, here are a few sites:

    jQuery

    https://api.jquery.com/jQuery.ajax/

    https://api.jquery.com/category/ajax/shorthand-methods/

    https://api.jquery.com/category/ajax/

    Vanilla JavaScript

    https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格