我很难在 XMLHttpRequest POST 后呈现页面。
在我的客户端,我执行以下操作:
var http = new XMLHttpRequest();
var url = window.location.origin + "/mypath";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
console.log('you should be redirected or render a new page);
} else {
// Handle error
}
}
http.send(JSON.stringify(myParams));
和服务器端:
router.get("/info", (req: Request, res: Response) => {
res.render('info', {msg: req.query.msg});
})
router.post("/mypath", (req: Request, res: Response, next) => {
res.send(res.redirect(url.format({
pathname:"/info",
query: {msg:'my message'}
})));
})
使用上面的代码不会重定向到信息页面。我做错了什么?
请注意,我不希望我的用户在他的浏览器导航栏中看到查询消息,而只是希望看到 /info
。
最佳答案
执行 ajaxes 时不能从服务器端重定向页面,您可以做的是在条件处于您希望的状态时从客户端更改 url,您可以使用以下命令从客户端重定向的任何方式。
let baseUrl = window.location.origin
window.location.replace(baseUrl + '/info');
关于javascript - 在 XMLHttpRequest POST 之后渲染页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44651377/