苹果手机端浏览器 localStorage.getItem('url','location.href') 存储失败 取到的是 null 为什么怎么解决
时间: 2023-11-25 21:04:49 浏览: 31
可能是因为你的代码有误。localStorage.getItem() 方法只接受一个参数,即要检索的键名。你可以尝试以下代码:
```
localStorage.setItem('url', location.href);
var url = localStorage.getItem('url');
console.log(url);
```
如果还是无法解决问题,可能是由于浏览器的隐私模式或者安全设置禁止了 localStorage 存储。你可以尝试更换浏览器或者在设置中禁用该选项。
相关问题
$('.name').html('用户名' + localStorage.getItem("username")) $('.exit').on('click', function () { localStorage.removeItem('username'); window.location.href = "Ego\homepage.html" }) let userId = localStorage.getItem('userId')将这段代码进行修改,换成能与fetch匹配的
好的,如果您想将这段代码修改成能够与 Fetch API 匹配的形式,可以按照以下步骤进行操作:
1. 首先,需要定义一个函数来获取用户数据:
```
function getUserData(userId) {
return fetch(`http://finalShop.lzaly.cn/user/getUserById/${userId}`)
.then(response => response.json())
.then(data => {
return data;
})
.catch(error => {
console.error(error);
return null;
});
}
```
这个函数接受一个用户 ID 作为参数,然后使用 Fetch API 发送请求获取用户数据。如果请求成功,会将响应数据解析为 JSON 格式,并返回解析后的数据。如果请求失败,会将错误打印到控制台上,并返回 null。
2. 接下来,可以定义一个函数来渲染用户数据:
```
function renderUser(user) {
$('.name').html('用户名' + user.username);
$('.exit').on('click', function () {
localStorage.removeItem('userId');
window.location.href = "Ego\homepage.html";
});
}
```
这个函数接受一个用户数据对象作为参数,并根据数据对象来渲染页面。由于具体的页面结构和渲染方式可能会有所不同,因此这里只能提供一个示例代码。
3. 最后,可以将这两个函数组合起来,实现用户数据的获取和渲染:
```
const userId = localStorage.getItem('userId');
getUserData(userId)
.then(user => {
if (user) {
renderUser(user);
}
});
```
这段代码首先从本地存储中获取用户 ID,然后调用 `getUserData` 函数来获取用户数据。如果获取成功,会将数据传递给 `renderUser` 函数来进行渲染。如果获取失败,则不会进行渲染。
希望这些信息能够帮到您。
<%@ include file = "jsp/utils/menu.jsp" %> <%@ include file = "jsp/static/setMenu.js" %> <%@ include file = "jsp/utils/baseUrl.jsp" %> // 用户登出 <%@ include file = "jsp/static/logout.jsp" %> $(document).ready(function () { //我的后台,session信息转移 if (window.localStorage.getItem("Token") != null && window.localStorage.getItem("Token") != 'null') { if (window.sessionStorage.getItem("token") == null || window.sessionStorage.getItem("token") == 'null') { window.sessionStorage.setItem("token", window.localStorage.getItem("Token")); window.sessionStorage.setItem("role", window.localStorage.getItem("role")); window.sessionStorage.setItem("accountTableName", window.localStorage.getItem("sessionTable")); window.sessionStorage.setItem("username", window.localStorage.getItem("adminName")); } } $('.dropdown-toggle .hidden-xs').html(window.sessionStorage.getItem('username')) $('.copyright').html('欢迎使用' + projectName) var token = window.sessionStorage.getItem("token"); if (token == "null" || token == null) { alert("请登录后再操作"); window.location.href = ("jsp/login.jsp"); } setMenu(); if(window.sessionStorage.getItem('role') != '管理员'){ var accountTableName = window.sessionStorage.getItem('accountTableName'); $('#myinfo').attr('href', baseUrl + 'jsp/modules/' + accountTableName + '/add-or-update.jsp'); http(accountTableName+'/session','GET',{},(res)=>{ if(res.code == 0){ window.sessionStorage.setItem('id',res.data.id); window.sessionStorage.setItem('onlyme',true); } }); } });
这段代码主要是前端页面的逻辑代码,包括以下部分:
- `include` 标签用于引入其他 JSP 页面,例如 `menu.jsp`、`setMenu.js`、`baseUrl.jsp`、`logout.jsp` 等;
- `$(document).ready(function () { ... });` 表示页面加载完成后执行的代码,主要用于初始化页面;
- `window.sessionStorage` 和 `window.localStorage` 分别是浏览器提供的会话存储和本地存储对象,用于存储用户信息和其他数据;
- `$('.dropdown-toggle .hidden-xs').html(window.sessionStorage.getItem('username'))` 表示用当前用户的用户名来替换页面中 `class` 为 `dropdown-toggle` 和 `hidden-xs` 的元素的内容;
- `setMenu()` 表示调用 `setMenu.js` 中的方法来设置页面的菜单;
- `window.sessionStorage.getItem('role')` 表示获取当前用户的角色信息;
- `$('#myinfo').attr('href', baseUrl + 'jsp/modules/' + accountTableName + '/add-or-update.jsp');` 表示设置页面中 `id` 为 `myinfo` 的元素的 `href` 属性,用于跳转到当前用户的信息页面;
- `http(accountTableName+'/session','GET',{},(res)=>{ ... });` 表示调用 `http` 方法向服务器发送请求,并在回调函数中处理服务器的响应结果。
总的来说,这段代码主要用于初始化页面、获取用户信息、设置页面元素的内容和属性等操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)