Button onclick get request to invoke getIndex()

时间:2015-06-15 15:16:34

标签: javascript jquery node.js http get

I have a button which should perform a get request to '/index'. Then my page should call getIndex via a routing method, which should render index.html.

    <div>
        <script> function goToHome(){ 
            $.get("/index");
        }
        </script>

        <button type="button" onclick="goToHome()">
            Go to the dashboard!
        </button>   
    </div>

router.get('/index', Routemethods.getIndex);

exports.getIndex = function(req, res){
    res.render('index.html');
}

However, the request is made, and getindex is being called (logged to see if it did). The problem is that my page is not being reloaded to index.html. What am I doing wrong? I made it work using href but not a big fan of that approach.

1 个答案:

答案 0 :(得分:0)

您还没有指定这是浏览器上的节点还是jquery。这似乎是一个ajax调用,而不是导航。

   <script> function goToHome(){ 
        $.get("/index");
    }
    </script>

这将使您的浏览器请求页面

   function goToHome(){
         window.location.href = '/index'; // this might need tweaking based on your servers setup, paths, etc
   }

但如前所述,您可以使用链接和正确的href来完成此操作。除非有充分的理由,否则你不应该将它卸载到按钮和js上。您正在使用更多内存并使导航依赖于js。