提问人:gene 提问时间:6/8/2017 更新时间:1/27/2019 访问量:1494
MVC:如何根据页面的AJAX请求在浏览器中更改URL地址?
MVC: How to change URL address in browser based on the AJAX request for the page?
问:
我的 MVC 页面上有一个 AJAX 请求:
@Ajax.ActionLink("Link","Index","Page1",new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId= "mainAjax", LoadingElementId="loader", OnComplete="ChangeUrl();" })
单击链接时,页面加载了正确的内容,但不会更改。URL
单击 时,地址栏应有地址。Link
http://localhost:1111/Link
需要做些什么才能使用所请求页面的正确地址更新 URL?
答:
0赞
saeid mohammad hashem
1/27/2019
#1
您可以在单击事件上使用 jquery 绑定。对于示例,因此您可以向链接添加类属性,如下所示;
@Ajax.ActionLink("Link Title", "ActionName", "Controller", new { @class = "menuItems" })
添加点击事件;
$(document).ready(function () {
$(".menuItems").click(function() {
var href = $(this).attr("href");
if (href.indexOf("javascript") >= 0)
return;
history.pushState(null, $(this).html, href);
});
});
您必须注意传递给 pushState 函数的 href 参数。pushState 的此 URL 参数应相对于当前页面,或者是您自己域中的绝对 URL。你不能跨域推送状态 - 这将是一个重大的安全漏洞。 如果 URL 参数不正确,则会收到此错误“未捕获的 SecurityError:无法在”历史记录“上执行”pushState“:具有 URL 的历史记录状态对象”。
评论
window.history.pushState(null, null, url)
back