httprouter.GET无法正常工作

时间:2017-01-06 14:23:27

标签: go router

我有一个html表单,它使用get-method提交数据。

jQuery( document ).ready(function() {
      console.log( "ready!" );
      startparticle();
});

function startparticle(){
  var init = function(){
  var isMobile = navigator.userAgent &&
    navigator.userAgent.toLowerCase().indexOf('mobile') >= 0;
  var isSmall = window.innerWidth < 1000;
  
  var ps = new ParticleSlider({
    ptlGap: isMobile || isSmall ? 3 : 0,
    ptlSize: isMobile || isSmall ? 3 : 1,
    width: 1e9,
    height: 1e9
  });
    
  var gui = new dat.GUI();
  gui.add(ps, 'ptlGap').min(0).max(5).step(1).onChange(function(){
    ps.init(true);
  });
  gui.add(ps, 'ptlSize').min(1).max(5).step(1).onChange(function(){
    ps.init(true);
  });
  gui.add(ps, 'restless');
  gui.addColor(ps, 'color').onChange(function(value){
    ps.monochrome = true;
    ps.setColor(value);
    ps.init(true);
  });
  
  
  (window.addEventListener
   ? window.addEventListener('click', function(){ps.init(true)}, false)
   : window.onclick = function(){ps.init(true)});
}

var initParticleSlider = function(){
  var psScript = document.createElement('script');
  (psScript.addEventListener
    ? psScript.addEventListener('load', init, false)
    : psScript.onload = init);
  psScript.src = 'http://particleslider.com/js/particleslider/current/particleslider.js';
  psScript.setAttribute('type', 'text/javascript');
  document.body.appendChild(psScript);
}
    
(window.addEventListener
  ? window.addEventListener('load', initParticleSlider, false)
  : window.onload = initParticleSlider);
}

这是我的代码

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Web Search</title>
</head>
<body>
    <form action="/search" method="get">
        <label for="tags">Enter your tags(Comma separated)</label>
        <br>
        <input type="text" name="tags">
        <input type="submit">
    </form>

</body>
</html>

我希望这个代码只需在按下提交按钮时打印标签(func GrabQuestions最终将提供不同的任务,因此打印搜索键的另一种方法不是我想要的),但当我点击提交按钮,它会出现package main import ( "net/http" "log" "html/template" "fmt" "github.com/julienschmidt/httprouter" ) func main() { router := httprouter.New() router.GET("/",Search) router.GET("/search?key=:tags",GrabQuestions) log.Fatal(http.ListenAndServe(":8080",router)) } func Search(w http.ResponseWriter,r *http.Request,_ httprouter.Params){ t,err := template.ParseFiles("E:/work/src/github.com/krashcan/sos/template/index.html") if err!= nil{ log.Println(err) } t.Execute(w,nil) } func GrabQuestions(w http.ResponseWriter,r *http.Request,ps httprouter.Params){ fmt.Fprintf(w,"Tags were %s", ps.ByName("tags")) } 错误。我认为我有一些愚蠢的错误,但我只是没有看到它。我错过了什么?

1 个答案:

答案 0 :(得分:0)

正如弗兰克在评论中指出的那样,httprouter不支持通过“查询参数”进行路由,因此您应该将搜索路径定义更改为以下内容:

router.GET("/search", GrabQuestions)