gpt4 book ai didi

Javascript 页面重定向和 session

转载 作者:行者123 更新时间:2023-11-30 05:37:01 25 4
gpt4 key购买 nike

我有注册javascript功能。通过 windiw.location 函数重定向到用户(用户的私有(private)页面)是否正确?它会在 session 范围内工作吗?它会在重定向请求时附加 httpcookie 吗?

function signup()
{

var uName = document.forms[0].email.value;
var pass = document.forms[0].password.value;

var xmlhttp;
var response;
var url = "/v2/application/userlogin?fromClient=web&"+"email="+uName+"&password="+pass;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
response = xmlhttp.responseText;
//alert(response);
window.location = "/web/jsp/index.jsp?fromClient=web";

} else {
//document.getElementById("mainWindow").innerHTML = "Loading...";
//alert("loding");
}
};
xmlhttp.open("POST", url, true);
xmlhttp.send();


//alert(response);

}

最佳答案

从 ajax 回调中重定向是完全可以接受的。但是,当请求 protected 页面时,在服务器端,您必须确保用户在显示 protected 内容之前具有经过身份验证的 session 。

Will it append the httpcookie while redirecting the request?

如果服务器响应包含一个新的 cookie,那么它会在 window.location 请求发生时发送。

旁注:您应该使用 encodeURIComponent() 对 URL 中的用户输入进行 URL 编码,以避免特殊字符破坏 URL 编码格式:

var url = "/v2/application/userlogin?fromClient=web&email="+ encodeURIComponent(uName)+"&password="+encodeURIComponent(pass);

关于Javascript 页面重定向和 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23061193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com