当我登录系统时,向服务器发送用户名和密码并获得响应。然后我需要从响应数据中创建会话变量,这是我的代码
<script type="text/javascript">
function loginData() {
var loginObject = {
username: $("#username").val(),
password: $("#password").val(),
};
$.ajax({
type: "POST",
url: "http://enh-cis.cloudapp.net/e.cloud/api/UserLogin/GetUserLoginDetails",
data: loginObject,
dataType: 'json',
success: function (data) {
if (data != "") {
//here I need to create session variables
window.location.replace("@Url.Action("Index", "GRN")");
}
else {
alert("username or password is incorrect");
window.location.replace("@Url.Action("Login", "GRN")");
}
},
});
}
</script>
答案 0 :(得分:2)
sessionStorage
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
sessionStorage.setItem('loggedIn', true);
sessionStorage.getItem('loggedIn'); // true
sessionStorage.removeItem('loggedIn');
sessionStorage.getItem('loggedIn'); // null
// destroy all session storage
sessionStorage.clear();