如何路由到post请求中的页面,post请求本身是通过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
}
})
我试图做的是,当take为true时,客户端被重定向到路由"/",当taked为false时,客户端则被重定向到"/success"。我在做这些重定向时遇到了问题,如果有任何帮助,我们将不胜感激。谢谢
你不能用这种方式来做,至少如果我理解正确的话。目前的情况是浏览器接收到响应(/success或/)作为其XMLHTTP请求的答案。然而,此时重定向已经发生。xhr.request.body将包含任何GET/success和GET/return。
您可能想做的只是简单地返回一个状态代码(我认为良好的REST实践是403表示失败,200表示成功),并在客户端对此做出反应。
相关文章:
- 当我在form_for中的text_field_tag中点击Enter时,如何禁止发出POST请求
- 为什么 AJAX POST 请求不起作用
- Ajax POST请求没有'我不了解PHP
- 防止双击执行两次jQuery post请求
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 如何使用Javascript获取POST请求填充的元素的值
- NodeJS中POST请求的请求体为null
- 在Fiddler JavaScript中识别POST请求
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 通过AJAX向同一页面发送POST请求,并使用$_POST获取值
- Ajax Post请求不起作用
- 如何在cloud9中向server.js发送post请求
- mootools表单json post请求
- 可以'无法在CasperJS中复制POST请求
- node.js请求POST数组“;第一个参数必须是字符串或缓冲区;
- PythonBottle:访问请求POST数据中的数组变量
- NodeJS请求POST图标
- Yii框架2.0 AJAX请求POST方法