英文:
calling Golang functions from within javascript code
问题
我正在开发一个Web应用程序,已经有一个布局CSS和Bootstrap v.3,还有一个index.html文件。我已经成功使用Golang加载并运行了该项目。我嵌入了一个注册按钮,点击后应该调用server.go文件中处理HTTP请求的Go函数。
$(document).ready(function() {
$('#signup').on('click', loginHandler);
});
我有一个server.go文件,内容如下:
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) {
}
所以问题是,当点击一个具有signup Id的按钮实例时,我应该如何触发server.go文件中的golang loginHandler函数?对此有什么想法吗?
英文:
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
得分: 5
你要找的是叫做AJAX(Asynchronous Javascript And Xml)的东西。它是一种JavaScript技术,允许你通过异步HTTP请求从服务器获取数据。看起来你正在使用jQuery,并且使用jQuery与AJAX的结合,代码如下:
$.ajax({
url: "http://www.example.com/signup",
data: {username: "whatever"} //如果请求需要数据的话
}).done(function (data) {
// 对返回的数据进行处理
});
如果你愿意,你也可以使用专门用于GET
和POST
的函数:
$.get("url: http://www.example.com/signup", function (data) {
// 对返回的数据进行处理
});
$.post("url: http://www.example.com/signup", {username: "whatever"}, function (data) {
// 对返回的数据进行处理
});
甚至可以在没有jQuery的情况下执行AJAX:
var req = new XMLHTTPRequest();
req.addEventListener("load", function (data) {// 对返回的数据进行处理});
req.open("get", "http://example.com", true);
req.send();
如果你需要关于AJAX的参考资料,以下是一些网站:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论