子域上的 javascript pushState 抛出异常

标签 javascript

在我的 js 文件中:

window.history.pushState('','',slug);

我知道 pushState 方法的安全限制:

The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception.

但是,在我的网站中,我使用了一个域:www.mydomain.com,其中 pushState 工作正常。但是当我在我的子域 subdomain.mydomain.com 上调用该方法时,它会抛出一个奇怪的异常:

Uncaught SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'http://0.0.7.210/' cannot be created in a document with origin 'http://subdomain.mydomain.com'.

我确实将 IP 0.0.7.210 称为内部的东西,但我在开发 + 实时环境中遇到此异常。

顺便说一句,我确实通过 Route53 解析了我的子域。也许与此有关?

最佳答案

事实证明,推送一个额外的 / 解决了这个问题。

改变了这个

window.history.pushState('','',slug);

对此

window.history.pushState('','','/'+slug);

关于子域上的 javascript pushState 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31589310/

相关文章:

javascript - Material-ui <Grid item> 组件的子组件与父组件的宽度为 100% 重叠

javascript - 模块参数的 Node js module.exports

javascript - 从变量访问 JSON 值

javascript - 数组函数返回 null

javascript - 向 Rails Controller 发送 AJAX 请求,然后向外部服务发送 HTTParty,后者将 JSON 返回到 .html.erb 文件中的输出

javascript - HTML5 Canvas 设置顶部/左侧

javascript - 从用户控件内部关闭 jQuery 对话框

javascript - Multi-Device-Hybrid-Apps - 有谁获得了适用于 android 的 indexeddb 插件吗?

javascript - 数据表+RequireJS : Cannot read property 'defaults' of undefined

Javascript 选择具有包含单词的属性的 anchor