从javascript代码中调用Golang函数 [英] calling Golang functions from within javascript code

查看:1067
本文介绍了从javascript代码中调用Golang函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个已经有布局css,bootstrap v.3和index.html的webapp。我已经成功加载了Golang并启动并运行该项目。我已经嵌入了一个注册按钮,点击该按钮后,应该从处理http请求的server.go文件中调用Go函数。

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);
});

我有一个这样写的server.go文件:

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) {

}

所以问题是点击带有注册ID 的按钮实例,如何在server.go文件中触发golang loginHandler 功能?
任何想法都会受到赞赏。

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.

推荐答案

你要找的是AJAX( A 同步 J avascript A nd X ml)。它是一种JavaScript技术,允许您发出异步HTTP请求以从服务器获取数据。看来你正在使用jQuery,并使用jQuery与AJAX,看起来像这样:

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
});

如果需要,可以专门用于 GET POST

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甚至可以在没有jQuery的情况下执行:

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();

如果你需要AJAX的参考资料,这里有一些网站:

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/

香草JavaScript

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

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

这篇关于从javascript代码中调用Golang函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆