繁体   English   中英

XMLHttpRequest POST之后呈现页面

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM