Node.js 使用单页JQuery Mobile进行NodeJS重定向

Node.js 使用单页JQuery Mobile进行NodeJS重定向,node.js,jquery-mobile,express,Node.js,Jquery Mobile,Express,我正在使用jquerymobile(JQM)开发NodeJS/ExpressJS应用程序。从后端重定向时遇到问题。我的应用程序动态生成按钮 出席:“+numAttending.numAttnd+”人们要去 在进行$.getJSON搜索之后。使用promises/setTimeout,将以下处理程序附加到所有按钮: $('.setAttndingbtn').click(function(){ var buttonsir = $(this); //Store this button ref

我正在使用jquerymobile(JQM)开发NodeJS/ExpressJS应用程序。从后端重定向时遇到问题。我的应用程序动态生成按钮

出席:“+numAttending.numAttnd+”人们要去

在进行$.getJSON搜索之后。使用promises/setTimeout,将以下处理程序附加到所有按钮:

$('.setAttndingbtn').click(function(){
  var buttonsir = $(this);    //Store this button reference  
  if($(this).html().slice(0,1) == 'A'){  //Check the button's state
       $.getJSON('/api/attending/' + $(this).val(), function(data){ //Ajax
             $(buttonsir).text("You are attending! " + data.numAttnd + " people are going"); //Change state
                       });
  } else {
       $.getJSON('/api/attending/rmv/' + $(this).val(), function(data){
             $(buttonsir).text("Attending: " + data.numAttnd + " people are going");
                        });
                    } 
                });
有关路线如下:

function isLoggedIn(req, res, next){
    if(req.isAuthenticated()){
        return next();
    } else {
        res.redirect('/login');
    }
}

  app.route('/login')
  .get(function(req, res){
        res.sendFile(p + "/public/login.html");
  });

  app.route('/api/attending/:number')
  .get(isLoggedIn, searchServerInstance.setAttending);
在后端,当单击“参与”按钮时,我想检查用户是否已登录,如果未登录,则将其重定向到登录页面。当我按原样运行代码时,在Firefox的Firebug控制台中,我会看到GET请求,如果我展开它,在“响应”部分,会显示登录页面的HTML代码。但是页面实际上并没有加载文件

My/login.html具有JQM推荐的
、标题和内容标记。我尝试将整个登录页面包含在my/index.html中,但尝试使用
res.redirect(“/public/index.html#page2”)
会导致“找不到文件”


是否可以使用Node/Express告诉JQM要加载哪个
?有没有一种方法可以强制JQM从服务器端加载/login.html,就像
rel=“external”
允许的那样?还是我必须抛弃JQM?

阅读JQM文档,我找到了一些方法来实现这一点。对于其他遇到这种情况的人,我相信我能理解:

  • 如果您不关心维护单个页面的完整性,那么可以考虑关闭,比如关闭
    ajaxEnabled
    ,关闭JQM的哈希侦听和ajax,正常加载url。我不知道这是否适用于Express的res.redirect,因为我还没有尝试过
  • 我选择做的是将
    res.redirect('/login.html')
    替换为自定义JSON响应
    res.send({loginState:“login”})
    。然后,在客户端,在
    $('.setAttndngbtn')
    侦听器中的
    $.getJSON
    请求中,我放入以下代码:

  • 而且效果很好!以下是pagecontainer小部件上的JQM文档,它允许使用
    change
    方法进行页面内重定向,或使用
    load
    方法进行外部重定向。

    直接在执行重定向操作的else部分发送文件怎么样。请检查。@GandalftheWhite我刚才尝试了'isLoggedIn(){…}或者{res.sendFile(p+'/public/login.html')},但遇到了同样的问题,控制台显示的html文件与GET响应中的一样,但是页面没有加载它。所以我猜是xD。您在前端使用的是什么?JQuery Mobile的影响最大。但是在/index.html页面的头部还包括jquery、jquery ui和bootstrap的js和css文件/login.html在data role=“header”中有bootstrap social和font-awesome。
    if(data.loginstate !== "LOGIN"){
              $(buttonsir).text("You are attending! " + data.numAttnd + " people are going");
    } else {
              $(":mobile-pagecontainer").pagecontainer("change", '#page2');
    }