我正在使用JQuery Mobile(JQM)開發NodeJS/ExpressJS應用程序。我無法從後端重定向。我的應用程序一個$ .getJSON搜索作出後動態生成按鈕NodeJS單向頁面重定向JQuery Mobile
<button class=\"setAttndingbtn btn btn-primary\" value=\"" + curr.place_id + "\">Attending: " + numAttending.numAttnd + " people are going</button>
。使用承諾/ 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代碼。但是頁面並沒有實際加載文件。
我的/login.html包含<div data-role="page">
,標題和內容標籤,正如JQM建議的那樣。我試過在我的/index.html中包含整個登錄頁面,但試圖在「未找到文件」中使用res.redirect("/public/index.html#page2")
結果。
是否可以使用Node/Express來告訴JQM要加載哪個<div data-role="page" id="page1">
?有沒有辦法強制JQM從服務器端加載/login.html,方式rel="external"
允許?或者我會不得不拋棄JQM?
如何直接發送文件在重定向事件完成的其他部分。請檢查。 –
@GandalftheWhite我剛剛嘗試過'isLoggedIn(){...} else {res.sendFile(p +'/public/login.html')}並且遇到同樣的問題,控制檯顯示HTML文件和GET響應一樣,但該頁面不會加載它。 –
所以我猜想xD。你在前端使用什麼? –