【发布时间】:2019-03-14 16:06:31
【问题描述】:
在我使用 Ajax 更改我的 DOM 之后,我也尝试更改 URL(不刷新页面)。
$("nav li").click(function(e) {
e.preventDefault();
var url;
var targetLocation = this.parentNode.attributes.href.value;
switch (targetLocation) {
case "index.html": url = "http://localhost:8080/home"; break;
case "app.html": url = "http://localhost:8080/api"; break;
case "contact.html": url = "http://localhost:8080/contact"; break;
}
$.ajax({
type: "GET",
dataType: "text",
url: url,
context: document.body,
success: function(data) {
$("div").text(data);
history.pushState("", "", targetLocation);
}
})
})
但我收到一条错误消息:
Uncaught DOMException: failed to execute "pushState" on "History": A history state object with URL "file:///C:/test/app.html" cannot be created in a document with origin "null" and URL "file:///C:/test/index.html".
【问题讨论】:
标签: javascript jquery ajax html5-history