如果用户点击按钮,则会发出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
}
})
我尝试做的是当采取的是真的,然后客户端被重定向到路线' /'当被采取是假的,然后客户被重定向到' /成功'。我在执行这些重定向时遇到问题,我们将不胜感激。感谢。
答案 0 :(得分:0)
你不能以这种方式去做,至少不是如果我正确理解你的话。当前发生的事情是浏览器接收响应(/ success或/)作为其XMLHTTP请求的答案。然而,重定向已经发生在这一点上。 xhr.request.body将包含任何GET /成功和GET / return。
你可能想做的只是返回一个状态代码(我相信好的REST实践将失败403,成功200)并对客户端做出反应。