簡體   English   中英

如何路由到通過XHR請求本身調用的發布請求中的頁面?

[英]How to route to a page in a post request that is itself called through an XHR request?

如果用戶單擊按鈕,則會發出xhr請求以轉到POST:

        var xhr = new XMLHttpRequest();   // new HttpRequest instance 
        xhr.open("POST", "/registration");
        xhr.addEventListener("load", e => {
            console.log(xhr.responseText)
        });

        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        xhr.send(JSON.stringify(this.state));

這是POST請求。

router.post('/registration', function *(req, res, next) {
console.info("trying to get the user");
//console.log(this.request.body);

let {name, password} = this.request.body

let taken = yield utils.nameTaken(name)

if(taken){
    //redirect to reg page
    console.log("taken!");
    //this.response.redirect("/registration");
    //res.redirect('/');
    console.log(req);

}
else{
    userSchema.createUser(name, password, function(err, user){
    //console.log(err, user);
    });
    //this.response.redirect("/success");
    //redirect to success page
}

})

我想做的是,當采用true時,客戶端被重定向到路由“ /”,當采用false時,客戶端被重定向到路由“ /成功”。 我在執行這些重定向時遇到了麻煩,我們將不勝感激。 謝謝。

您無法以這種方式進行操作,至少在我理解正確的情況下無法這樣做。 當前發生的情況是瀏覽器收到響應(/成功或/)作為其XMLHTTP請求的答案。 但是,重定向已經在這一點上發生了。 xhr.request.body將包含任何GET /成功和GET /返回。

您可能想要做的就是簡單地返回一個狀態碼(我相信良好的REST做法對於失敗是403,對於成功是200),然后對客戶端做出反應。

暫無
暫無

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

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