簡體   English   中英

res.redirect() 沒有將我重定向到任何頁面,沒有錯誤,有人可以解釋這里發生了什么嗎?

[英]res.redirect() is not redirecting me to any page ,there are no errors ,can someone explain whats happening here?

一切都按預期工作,但是,我希望我的用戶在發送消息后立即切換到不同的 url,因此在他向服務器發送 ajax 發布請求后,我試圖將他/她從服務器中的 res 變量重定向. 我確信 client.js 沒有任何問題,但是 res.redirect 將我重定向到任何地方,我在同一頁面上,在消息發送后。沒有錯誤。 並且兩個 console.log 語句都在執行,所以 res.redirect 可能有問題,請有人告訴我該怎么做。

基本信息:我在客戶端使用快遞服務器、nodejs 和 javascript。

客戶端.js

$(document).ready(
    message=''
);
function getmessagefrominput(){
    var message =$('#message');
    var value = message.val();
    message.val('');
    return value;
}

function appendmessage(message){
    var messagecontainer =document.getElementById('messages');
    //console.log(messagecontainer);
    var mymessage= document.createElement("div")
    mymessage.setAttribute('class','sentmessagecontainer')
    var newmessage= document.createElement("p")
    newmessage.setAttribute('class','sent')
    newmessage.textContent=message;
    mymessage.appendChild(newmessage);
    messagecontainer.appendChild(mymessage);
}
function sendmessage(username,friend){
    var message=getmessagefrominput();
    //console.log('{from:'+username+',to:'+friend+',message:'+message+'}')
    if(message!=null && message!=''){        
        var jqxhr =$.post( "/ajax/newmessage", { from:username,to:friend,message:message } , ()=>{appendmessage(message);});
    }  
}

服務器.js

console.log('trying to redirect');
    res.redirect('/'+obj.from+'/direct/inbox/'+x);
    console.log('ok redrected')

;

當您執行 ajax 時,它會向服務器發起一個單獨的請求,該重定向不會影響主線程(無論 ajax 請求上的重定向,頁面都保持不變)。

如果要重定向頁面,則需要在 Ajax 回調中使用document.location.href或使用<form>請求,這會將您的頁面替換為可能受重定向影響的請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM