简体   繁体   English

JavaScript 本地存储在 Chrome 和 Edge 中删除,但不在 Firefox 中

[英]JavaScript Local Storage removes in Chrome and Edge but not within Firefox

My current problem is that the local storage is persisting data that needs to be removed if the user encounters an error by entering an incorrect postcode within the URL.我当前的问题是,如果用户在 URL 中输入不正确的邮政编码而遇到错误,本地存储会保留需要删除的数据。 In Chrome and Edge, this is working as expected.在 Chrome 和 Edge 中,这按预期工作。

Firstly I have a global variable of apiFailed that is set to false.首先,我有一个全局变量 apiFailed 设置为 false。 I have weatherData that is an array of objects that is used to store and access the API data properties, I have storedData which is used for localStorage getItem() and setItem() and removeItem() properties and the savedWeatherData that gets assigned the storedData variable and is used to display and retrieve the persisted localStorage data.我有weatherData,它是一个用于存储和访问API 数据属性的对象数组,我有用于localStorage getItem() 和setItem() 和removeItem() 属性的storedData 以及分配给storedData 变量的savedWeatherData用于显示和检索持久化的 localStorage 数据。

On first running, as the localStorage has no data and is null, I check this and then assign the savedWeatherData variable the persisted data.在第一次运行时,由于 localStorage 没有数据并且为空,我检查了这一点,然后将savedWeatherData 变量分配给持久化数据。 I also check to ensure that apiFailed is false.我还检查以确保 apiFailed 为假。 I also refresh the page (via code), just to ensure that the data is retrieved.我还刷新页面(通过代码),只是为了确保检索到数据。 That code is found below here:该代码在下面找到:

if (savedWeatherData == null || savedWeatherData == undefined 
|| savedWeatherData === null || savedWeatherData === undefined) {
        if (apiFailed == false) {
          console.log('savedWeatherData is NuLL');
          storedData = JSON.parse(window.localStorage.getItem('data'));
          savedWeatherData = storedData;
          window.location.reload();

          if (navigator.userAgent.indexOf("Firefox") != -1) {
            console.log('You are in Firefox');
          }
        }
      } else {
        if (apiFailed == false) {
          console.log('savedWeatherData is NOT NULL');
          // if (navigator.userAgent.indexOf("Firefox") != -1) {
        }
      }

This works as expected in all browsers, Chrome, Edge and Firefox.这在所有浏览器、Chrome、Edge 和 Firefox 中都按预期工作。 On first page start up, there is no localStorage data, so this gets assigned and the page refreshes.在第一页启动时,没有 localStorage 数据,因此它被分配并刷新页面。 Upon refresh, localStorage is no longer null and has been assigned to savedWeatherData.刷新后,localStorage 不再为 null 并已分配给 savedWeatherData。 Which is then used throughout the application to retrieve and display data.然后在整个应用程序中使用它来检索和显示数据。

However, if the user enters a wrong postcode at any point, say entering a special character by mistake, Chrome and Edge run the code below as expected, displaying an error page instead and not fetching any data.但是,如果用户在任何时候输入了错误的邮政编码,比如错误地输入了一个特殊字符,Chrome 和 Edge 会按预期运行下面的代码,而是显示错误页面而不是获取任何数据。

  console.log('APIFAILED status ' + apiFailed);
  if (apiFailed == true) {
    storedData = window.localStorage.removeItem("data");
    savedWeatherData = storedData;
  }

Here comes my problem, in Firefox, the console.log of apiFailed shows that this is still false.我的问题来了,在Firefox中,apiFailed的console.log显示这仍然是错误的。 Therefore the localStorage is not removed.因此 localStorage 不会被删除。 This means that the error page does not display in Firefox, instead of an error message displaying like in Chrome and Edge, because the localStorage still has the data, this data gets displayed as if the user entered a correct postcode, even though they have not.这意味着错误页面不会在 Firefox 中显示,而不是像 Chrome 和 Edge 中那样显示错误消息,因为 localStorage 仍然有数据,这些数据会像用户输入正确的邮政编码一样显示,即使他们没有. The data persisted by the localStorage is not getting removed in the localStorage of Firefox. localStorage 保存的数据不会在 Firefox 的 localStorage 中删除。 It does get removed from the localStorage of Chrome and Edge, I see that in the Developer console.它确实从 Chrome 和 Edge 的 localStorage 中删除了,我在开发人员控制台中看到了这一点。

Here is how I get my error page to display.这是我如何显示我的错误页面。 (This is done by using the Phaser 3 Framework): (这是通过使用 Phaser 3 框架完成的):

if (apiFailed == true) {
    background = this.add.image(this.game.canvas.width / 2, this.game.canvas.height / 2, "sorryBackground");
    var apiFailText = this.add.text(150, this.game.canvas.height / 2 - 100, "Sorry. The weather forecast is currently unavailable",
      { fontFamily: "Open Sans, sans-serif", fontSize: 70, }).setOrigin(0, 0);
    storedData = window.localStorage.removeItem("data");
    savedWeatherData = storedData;
  }

I should also mention that apiFailed gets set to true in the catch after the fetch and retrieving and setting up the API data.我还应该提到 apiFailed 在获取、检索和设置 API 数据之后的 catch 中设置为 true。

Here:这里:

        .catch(() => {
      console.log(".catch() called. Error within the .fetch()");
      apiFailed = true;
    });

Any help and feedback would be appreciated.任何帮助和反馈将不胜感激。

Thanks.谢谢。

I solved this issue.我解决了这个问题。

I noticed in Firefox the issue was being caused as it was receiving an 400 error from my API fetch request.我注意到在 Firefox 中,问题是由于它从我的 API 获取请求中收到 400 错误而引起的。 This was not the case in Chrome and Edge. Chrome 和 Edge 并非如此。 Therefore, In the end I modified my fetch so that with the response I could see if I was getting any errors.因此,最后我修改了我的 fetch 以便通过响应我可以看到我是否收到任何错误。 If it does get any errors, all I do is simply clear out the entire localStorage and set apiFailed to true, which in turn runs the block of code to display the error page and message.如果确实出现任何错误,我所做的只是清除整个 localStorage 并将 apiFailed 设置为 true,然后运行代码块以显示错误页面和消息。

Here is the if statement within my fetch:这是我的 fetch 中的 if 语句:

 if (!response.ok) {
        apiFailed = true;
        window.localStorage.clear();
   }

Here is the if statement that displays the error page.这是显示错误页面的 if 语句。 Just in case, I do the same here by removing the stored API data within the localStorage and just set my savedWeatherData variable to this.为了以防万一,我在这里通过删除 localStorage 中存储的 API 数据来做同样的事情,并将我的 savedWeatherData 变量设置为这个。

if (apiFailed == true) {
    background = this.add.image(this.game.canvas.width / 2, 
    this.game.canvas.height / 2, "sorryBackground");
    var apiFailText = this.add.text(150, this.game.canvas.height / 2 - 100, 
      "Sorry. The weather forecast is currently unavailable.", { fontFamily: 
      "Open Sans, sans-serif", fontSize: 70, }).setOrigin(0, 0);
    storedData = window.localStorage.removeItem('data');
    savedWeatherData = storedData;
  }

After testing this is now looking to work within all major browsers: Chrome, Edge and Firefox经过测试,现在希望在所有主要浏览器中都能正常工作:Chrome、Edge 和 Firefox

I solved this issue.我解决了这个问题。

I noticed in Firefox the issue was being caused as it was receiving an 400 error from my API fetch request.我注意到在 Firefox 中,问题是由于它从我的 API 获取请求中收到 400 错误而引起的。 This was not the case in Chrome and Edge. Chrome 和 Edge 并非如此。 Therefore, In the end I modified my fetch so that with the response I could see if I was getting any errors.因此,最后我修改了我的 fetch 以便通过响应我可以看到我是否收到任何错误。 If it does get any errors, all I do is simply clear out the entire localStorage and set apiFailed to true, which in turn runs the block of code to display the error page and message.如果确实出现任何错误,我所做的只是清除整个 localStorage 并将 apiFailed 设置为 true,然后运行代码块以显示错误页面和消息。

Here is the if statement within my fetch:这是我的 fetch 中的 if 语句:

   if (!response.ok) {
        apiFailed = true;
        window.localStorage.clear();
   }

Here is the if statement that displays the error page.这是显示错误页面的 if 语句。 Just in case, I do the same here by removing the stored API data within the localStorage and just set my savedWeatherData variable to this.为了以防万一,我在这里通过删除 localStorage 中存储的 API 数据来做同样的事情,并将我的 savedWeatherData 变量设置为这个。

  if (apiFailed == true) {
    background = this.add.image(this.game.canvas.width / 2, 
    this.game.canvas.height / 2, "sorryBackground");
    var apiFailText = this.add.text(150, this.game.canvas.height / 2 - 100, 
      "Sorry. The weather forecast is currently unavailable.", { fontFamily: 
      "Open Sans, sans-serif", fontSize: 70, }).setOrigin(0, 0);
    storedData = window.localStorage.removeItem('data');
    savedWeatherData = storedData;
  }

After testing this is now looking to work within all major browsers: Chrome, Edge and Firefox经过测试,现在希望在所有主要浏览器中都能正常工作:Chrome、Edge 和 Firefox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript适用于IE和Edge,但不适用于Chrome和Firefox - JavaScript working on IE and Edge but not working on Chrome and Firefox Javascript 在 Edge 中不起作用,但在 Firefox、Chrome 中很好 - Javascript not working in edge, but fine in Firefox, Chrome JavaScript 代码在 Firefox 中有效,但在 Chrome 或 Edge 中无效 - JavaScript Code works in Firefox but not in Chrome or Edge JavaScript 在 Firefox、Chrome 中不起作用 - 在 IE、Edge 中有效 - JavaScript not working in Firefox, Chrome - works in IE, Edge Firefox和Chrome的持久本地存储? - Persistent local storage for both Firefox and Chrome? 边缘扩展chrome.storage.local 1MB限制 - Edge extendsion chrome.storage.local 1MB limit iframe 沙盒中的 WebSocket 在 Firefox 上失败,但适用于 Edge/Chrome - WebSocket within iframe sandbox fails on Firefox, but works on Edge/Chrome 本地视频不会在 Firefox 中循环播放,但可以在 Chrome (Javascript) 中使用 - Local video will not loop in Firefox, but works in Chrome (Javascript) JavaScript 行为差异:IE11/Edge 与 Chrome/Firefox - JavaScript Behavior difference: IE11/Edge vrs Chrome/Firefox $(document).ready Angular 项目代码中的 Javascript 适用于 Edge 但不适用于 chrome 或 firefox - $(document).ready Javascript in Angular project code works on Edge but not chrome or firefox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM