Search code examples
javascriptjqueryfirefoxsession-storage

Session Storage error on firefox only


My code works well in Chrome and Edge, but I'm getting this error in Firefox:

TypeError: 'key' called on an object that does not implement interface Storage

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);
        }
      });
    }
  });
}

Solution

  • You're attempting to pass the entire sessionStorage object in an AJAX request. Don't do that. Instead pull out the specific keys that you require. Something like this:

    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);
            }
          });
        }
      });
    }
    

    I'd also suggest that you return JSON instead of plain text, as the latter can lead to issues with whitespace causing unexpected results. It's also better typed, so you can return a boolean state flag, eg:

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