【问题标题】:requestStorageAccess Failure请求存储访问失败
【发布时间】:2020-08-27 07:32:25
【问题描述】:

当我在 iframe 中的页面上请求存储访问时,它返回“未定义”。

这意味着什么?我希望得到一个真实的,并且会理解错误。 (被拒绝)但是在这种情况下未定义是什么意思?出了什么问题?

<button id="start-button">Start Puctto</button>

<script>

function gain_access() {
  document.cookie = "foo=bar";              // set a cookie
  document.hasStorageAccess().then(hasAccess => {
    if (!hasAccess) {     
      return document.requestStorageAccess();
    }
  }).then(_ => {   
    // Now we have first-party storage access!
    // Let's access some items from the first-party cookie jar
    document.cookie = "foo=bar";              // set a cookie
    localStorage.setItem("username", "John"); // access a localStorage entry
    window.opener.postMessage('set_session', '*');   
    window.location.href = document.referrer;
  }).catch(_ => {
    alert(_);
  });
}
  document.getElementById('start-button').addEventListener('click', function (){
    console.log('Start button clicked');                
    gain_access();

  })
</script>

【问题讨论】:

    标签: javascript cookies safari storage-access-api


    【解决方案1】:

    我不相信有什么问题。您是否检查过该值是否设置正确?

    localStorage.setItem 即使有效也不会返回任何内容,所以当它打印到控制台时,它会显示 undefined 即它正在尝试打印返回值但没有返回值,因此返回值为 @987654324 @。

    不要依赖它的返回值来做其他事情,只是想当然。

    如果将值正确设置为localStorage,则一切正常。

    编辑:另外,显然请求存储访问的概念仍然是only at proposal stage,所以也许只是一起删除那部分。

    【讨论】:

    • 谢谢。 Safari 在没有请求存储访问权限的情况下不允许会话存储,因此尽管它有点测试版,但它是必需的。
    • 啊,我明白了 - 这是一个 Safari 的东西。我的坏,从来没有遇到过
    • 你会的,哈哈。它来到每个浏览器。
    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2019-06-15
    • 2021-03-21
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多