MVC - ajax 调用中的历史记录 API
MVC - History API in ajax call
我有一个 ajax 调用从一个页面移动到另一个页面。(在 div 中加载部分页面内容)我想更新一个 URL 基于这个 ajax 调用以便用户可以使用 back/forward 按钮。所以我正在使用历史 API.
例如
Search.cshtml
<input type="button" id="abc" />
js文件
$('#abc').click(function (e) {
$.ajax({
type: "POST",
url: "abccontroller/xyzaction",
cache: false,
datatype: "html",
success: function (data) {
window.history.pushState(
'abccontroller/xyzaction',
'Claim', // new page title
'abccontroller/xyzaction' // new url
)
$("#divMainContainer").html(data);
}
});
});
此 xyzaction returns 部分视图,我正在 div
中加载此 html
现在这会更新 URL 并正确移动到新页面。
但是当我点击浏览器后退按钮时,它不会加载上一页。它只会更改 URL(显示上一页 URL)。浏览器的前进按钮也有同样的问题。它不会移动到新页面。它只会改变 URL
我做错了什么?
也许你可以这样做:
$(window).bind('popstate', function(){
window.location.href = window.location.href;
});
对我有用。
此代码从 url 获取位置,然后将页面重定向到该位置。
我有一个 ajax 调用从一个页面移动到另一个页面。(在 div 中加载部分页面内容)我想更新一个 URL 基于这个 ajax 调用以便用户可以使用 back/forward 按钮。所以我正在使用历史 API.
例如
Search.cshtml
<input type="button" id="abc" />
js文件
$('#abc').click(function (e) {
$.ajax({
type: "POST",
url: "abccontroller/xyzaction",
cache: false,
datatype: "html",
success: function (data) {
window.history.pushState(
'abccontroller/xyzaction',
'Claim', // new page title
'abccontroller/xyzaction' // new url
)
$("#divMainContainer").html(data);
}
});
});
此 xyzaction returns 部分视图,我正在 div
中加载此 html现在这会更新 URL 并正确移动到新页面。
但是当我点击浏览器后退按钮时,它不会加载上一页。它只会更改 URL(显示上一页 URL)。浏览器的前进按钮也有同样的问题。它不会移动到新页面。它只会改变 URL
我做错了什么?
也许你可以这样做:
$(window).bind('popstate', function(){
window.location.href = window.location.href;
});
对我有用。
此代码从 url 获取位置,然后将页面重定向到该位置。