【问题标题】:Uncaught DOMException: Failed to execute 'pushState' on 'History':未捕获的 DOMException:无法在“历史”上执行“pushState”:
【发布时间】:2018-03-06 00:24:25
【问题描述】:

我有一个问题会根据用户的不同出现在不同的场景中。

A 组:当似乎选择的用户加载到主页并单击应该使用 AJAX 加载的导航锚点并使用 history.pushState('', '', destinationUrl);他们没有收到任何回应。控制台中显示错误消息:

未捕获的 DOMException:无法在“历史记录”上执行“pushState”:A 不能使用 URL 为“http://example.net/foo”的历史状态对象 在具有源“http://www.example.net”和 URL 的文档中创建 'http://www.example.net/'

根据搜索,我认为问题不会在 example.net/foo 中包含 www。我将导航锚点更新为“http://www.example.net/foo”。

这解决了 A 组的问题,但为更大的用户群 B 组创建了相同的问题,但出现了类似的错误:

未捕获的 DOMException:无法在“历史”上执行“pushState”:A URL 'http://www.example.net/foo' 的历史状态对象不能是 在具有源“http://example.net”和 URL 的文档中创建 'http://example.net/'。

对于 B 组,url 差异是倒置的。对于这两个组来说,这个问题似乎与浏览器无关。该网站使用 Craft CMS。

对可能发生的事情有任何方向或解决方案,我该如何解决这两个群体的问题?

【问题讨论】:

  • 更改您的服务器软件以将用户定向到单个域。例如,对于 Apache,您可以使用 mod_rewrite 将输入为 example.com 的任何 url 重定向到 www.example.com,反之亦然,无论您喜欢哪种方式。或者在javascript中根据当前域构建destinationUrl

标签: javascript jquery ajax html5-history


【解决方案1】:

感谢@Patrick Evans。使用 mod_rewrite 的建议将我带到这里:https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost

我最终使用了“if”指令推荐,但也测试了 mod_rewrite 并且效果很好。

    <If "%{HTTP_HOST} != 'www.example.net'">
      Redirect "/" "http://www.example.net/"
    </If>

【讨论】:

    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2015-12-07
    • 2019-08-16
    • 1970-01-01
    • 2022-06-17
    相关资源
    最近更新 更多