【问题标题】:MS Edge won't save cookies returned from AJAX request in local HTML fileMS Edge 不会将 AJAX 请求返回的 cookie 保存在本地 HTML 文件中
【发布时间】:2016-05-02 00:42:05
【问题描述】:

以下是我正在加载到 Microsoft Edge 中的本地文件的精简版本:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>

    <script>
      var url = "http://example.com"

      // Document is ready
      ready(function () {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true)
        xhr.withCredentials = true
        xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8")
        xhr.onload = function() {
          console.log('onload')
          console.log('Status: ' + xhr.status + ". " + xhr.statusText)
        }
        xhr.onerror = function(error) {
          console.log('onerror')
        }
        xhr.send()
      })

      // Hook for when the DOM has finished loading
      function ready(fn) {
        if (document.readyState !== 'loading') {
          fn()
        } else {
          document.addEventListener('DOMContentLoaded', fn)
        }
      }
    </script>
  </head>
  <body>
  </body>
</html>

使用file:///path/to/file.html 将其加载到浏览器中效果很好,并且请求成功返回。我遇到的问题是响应应该在浏览器中设置一个 cookie。它可以在 Chrome、IE11、Firefox 和 Safari 上完美运行,但在 Edge 上却不行。

是否有某种安全措施可以防止 Edge 从本地文件运行的 JavaScript 创建 cookie?如果有,为什么?

【问题讨论】:

  • 看起来如果您无法从另一个域创建 cookie.. 只是一个安全策略?
  • 我不这么认为,因为如果我运行从本地主机托管的文件,或者通过控制台在任何其他网页上运行脚本,它会完美运行。
  • 我有同样的问题 - 我发出一个 ajax 请求,响应包含一个 cookie。此 cookie 未保存在 Edge 中,但保存在 Chrome 和 Firefox 中

标签: javascript ajax cookies microsoft-edge


【解决方案1】:

Microsoft Edge 从不保存来自

的 javascript cookie
  • 直接打开本地文件,例如file://C:/test/index.html
  • 包含下划线的域,例如test_a.mydomain.com

为了测试 cookie,必须创建一个工作环境,例如本地主机@xampp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2013-10-17
    • 2022-01-04
    • 2012-02-11
    相关资源
    最近更新 更多