0

I'm writing a simple web application using Nodejs, Express, and Jade. I've set up my routes appropriately. I'm having a problem with navigating between views. I am able to change views by entering the appropriate url in my browser. How can I get my application to change views on button click? I realize this may stem from a fundamental misunderstanding of the technologies I'm using.

Currently, I am trying to hit my controller via jquery ajax

$('#submit').click(function(){
  $.ajax({
     url: "/go",
     data: "site=" + $('#website').val() + "&img=" + $('#image').val()
  });

});

This successfully hits my controller:

app.get('/go', function(req, res){
    res.render('view', {title: 'create', site: req.query['site'] });

This returns the proper html, however it does not get rendered as a new view.

4

1 回答 1

1

这将严重依赖于您的客户端代码是如何设置的(例如,您是否使用像 Backbone 这样的框架?)。最简单的方法就是为导航按钮设置点击处理程序,只需更改window.location.href为与相应视图关联的 URL。当然,这将导致在每次视图更改时重新加载完整的页面,这可能是可取的,也可能不是可取的,但是如果不了解更多关于应用程序的结构,没有人可以说更多。

或者,您可以使用良好的旧链接进行导航,并使用 CSS 使它们看起来像按钮。

请注意,这些注意事项与您使用的服务器端技术无关。

于 2012-08-18T15:52:06.013 回答