我正在开发一个已经有布局 css、bootstrap v.3 和 index.html 的 webapp。我已经在 Golang 启动并运行的情况下成功加载了项目。我已经嵌入了一个注册按钮,点击后应该从处理 http 请求的 server.go 文件中调用 Go 函数。
$(document).ready(function() {
$('#signup').on('click', loginHandler);
});
Run Code Online (Sandbox Code Playgroud)
我有一个 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) {
}
Run Code Online (Sandbox Code Playgroud)
所以问题是在单击带有signup Id的按钮实例时,我该如何触发server.go 文件中的 golang loginHandler函数?对此的任何想法将不胜感激。
你在找什么叫AJAX(一同步Ĵ avascript一个次X毫升)。它是一种 JavaScript 技术,允许您发出异步 HTTP 请求以从服务器获取数据。看起来您正在使用 jQuery,并且将 jQuery 与 AJAX 一起使用,看起来像这样:
$.ajax({
url: "http://www.example.com/signup",
data: {username: "whatever"} //If the request needs any data
}).done(function (data) {
// Do whatever with returned data
});
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以使用专门用于GETand 的函数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
});
Run Code Online (Sandbox Code Playgroud)
AJAX 甚至可以在没有 jQuery 的情况下执行:
var req = new XMLHTTPRequest();
req.addEventListener("load", function (data) {// Do whatever});
req.open("get", "http://example.com", true);
req.send();
Run Code Online (Sandbox Code Playgroud)
如果您需要 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
| 归档时间: |
|
| 查看次数: |
6349 次 |
| 最近记录: |