admin管理员组文章数量:1402807
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/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.
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./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.
Share Improve this question asked Mar 30, 2015 at 18:27 mehdix_mehdix_ 4791 gold badge8 silver badges20 bronze badges 01 Answer
Reset to default 5What 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./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./signup", function (data) {
// Do whatever with the returned data
});
$.post("url: "http://www.example./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.", true);
req.send();
If you need a reference for AJAX, here are a few sites:
jQuery
https://api.jquery./jQuery.ajax/
https://api.jquery./category/ajax/shorthand-methods/
https://api.jquery./category/ajax/
Vanilla JavaScript
https://developer.mozilla/en-US/docs/AJAX/Getting_Started
https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
本文标签: gocalling Golang functions from within javascript codeStack Overflow
版权声明:本文标题:go - calling Golang functions from within javascript code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744353785a2602201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论