[英]Render page after XMLHttpRequest POST
在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'}
})));
})
使用上面的代碼不會重定向到信息頁面。 我究竟做錯了什么?
請注意,我不希望我的用戶在他的瀏覽器導航欄中看到查詢msg,而只是看到/info
。
您無法在執行Ajax時從服務器端重定向頁面,您可以做的是在條件處於所需狀態時從客戶端更改url,可以使用以下命令從客戶端重定向任何方式。
let baseUrl = window.location.origin
window.location.replace(baseUrl + '/info');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.