在我的 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/