【问题标题】:Session Storage error on firefox only仅 Firefox 上的会话存储错误
【发布时间】:2017-12-01 06:55:25
【问题描述】:

我的代码在 Chrome 和 Edge 中运行良好,但在 Firefox 中出现此错误:

TypeError: 在未实现接口 Storage 的对象上调用了“key”

function goToNextStep() {
  $.post("../manager.php", {
    dados: sessionStorage,
    action: "save"
  }, function(response) {
    if (response === "1") {
      $.ajax({
        type: "GET",
        url: "../final.php",
        success: function(data) {
          $('#content').html(data);
        }
      });
    }
  });
}

【问题讨论】:

    标签: javascript jquery firefox session-storage


    【解决方案1】:

    您正试图在 AJAX 请求中传递 整个 sessionStorage 对象。不要那样做。而是拉出您需要的特定键。像这样的:

    function goToNextStep() {
      $.post("../manager.php", {
        dados: {
          foo: sessionStorage.getItem('foo'),
          bar: sessionStorage.getItem('bar')
        },
        action: "save"
      }, function(response) {
        if (response === "1") {
          $.ajax({
            type: "GET",
            url: "../final.php",
            success: function(data) {
              $('#content').html(data);
            }
          });
        }
      });
    }
    

    我还建议您返回 JSON 而不是纯文本,因为后者可能会导致空白问题,从而导致意外结果。它的类型也更好,因此您可以返回布尔状态标志,例如:

    if (response.success) {
      // your logic here...
    }
    

    【讨论】:

      【解决方案2】:

      我可以通过以下解决方法解决这个问题:

      function goToNextStep() {
        $.post("../manager.php", {
          dados: JSON.parse(JSON.stringify(localStorage)), // <----- change
          action: "save"
        }, function(response) {
          if (response === "1") {
            $.ajax({
              type: "GET",
              url: "../final.php",
              success: function(data) {
                $('#content').html(data);
              }
            });
          }
        });
      }
      

      【讨论】:

        猜你喜欢
        • 2016-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 2013-09-23
        • 2015-10-01
        • 2021-02-19
        • 1970-01-01
        相关资源
        最近更新 更多