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

从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.

图片转代码服务由CSDN问答提供 功能建议

我正在使用一个Web应用程序,该Web应用程序已经具有布局CSS,bootstrap v.3和index.html 。 我已经使用Golang成功加载并运行了该项目。 我已经嵌入了一个注册按钮,单击该按钮应该可以从处理HTTP请求的server.go文件中调用Go函数。

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

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

 包main 
 
import(
“ net / http” 
 
“ github.com/bmizerany/pat” 
)\  n 
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。 路径[1:])
} 
 
func loginHandler(w http.ResponseWriter,r * http.Request){
 
} 
   
 
 

所以 问题是单击带有 signup ID 的按钮实例时,如何在server.go文件中触发golang loginHandler 函数? 关于此的任何想法都是 表示赞赏。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题