Javascript Ajax.BeginForm与BeforeSend
我的MVC网站上有几个Ajax.BeginForm。同时,我需要在发送Ajax调用之前处理事件 下面的代码适用于我的手动jquery ajax调用,但不适用于ajax.BeginForm helpers:Javascript Ajax.BeginForm与BeforeSend,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我的MVC网站上有几个Ajax.BeginForm。同时,我需要在发送Ajax调用之前处理事件 下面的代码适用于我的手动jquery ajax调用,但不适用于ajax.BeginForm helpers: $.ajaxSetup({ 'beforeSend': function (xhr) { alert(''); } }); MVC Ajax.BeginForm上是否仍有处理beforeSend事件的方法 ---------------------------
$.ajaxSetup({
'beforeSend': function (xhr) {
alert('');
}
});
MVC Ajax.BeginForm上是否仍有处理beforeSend事件的方法
-------------------------------------------编辑-------------------------------------
我需要发送前事件,因为我要更改请求头:
'beforeSend': function (xhr) {
securityToken = $('[name=__RequestVerificationToken]').val();
xhr.setRequestHeader('__RequestVerificationToken', securityToken);
}
感谢
Ajax。BeginForm
您可以使用:
更新。要添加新的请求标头,可以执行以下操作:
function requestBeginHandler(ajaxContext) {
var request = ajaxCOntext.get_request();
securityToken = $('[name=__RequestVerificationToken]').val();
request.get_headers()['__RequestVerificationToken'] = securityToken;
}
我认为您所关注的示例并不涉及对Ajax表单集成的支持。我做了一些测试,找到了解决方案 我假设您使用MVC4时引用了
jquery-1.9.1.js
,jquery.validate.unobtrusive.js
和jquery.unobtrusive ajax.js
下面是我的代码
@model WebApplication1.Models.DummyModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
</head>
<body>
<div id="Parent">
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "post", OnBegin = "BeginClient" }))
{
@Html.AntiForgeryToken();
<div>First Name</div><div>@Html.TextAreaFor(m => m.FirstName)</div>
<div>Last Name</div><div>@Html.TextAreaFor(m => m.LastName)</div>
<input type="submit" value="Submit" />
}
</div>
<script type="text/javascript">
function BeginClient(xhr) {
alert("posting...");
securityToken = $('[name=__RequestVerificationToken]').val();
xhr.setRequestHeader('__RequestVerificationToken', securityToken);
}
$.ajaxSetup({
'beforeSend': function (xhr) {
securityToken = $('[name=__RequestVerificationToken]').val();
alert(securityToken);
xhr.setRequestHeader("__RequestVerificationToken", securityToken);
}
});
</script>
</body>
</html>
通过这种方式,您仍然可以强制在Ajax表单中使用防伪令牌
希望这有帮助。可能重复-发送前与开始时相同?因为我需要在更改之前更新请求头。看看我的问题。Thanks@hugohilario,不,这些是不同的功能。在意识到您的目的后,我删除了重构建议。您仍然可以访问request对象并添加一个标题,并使用可能的解决方案更新帖子。虽然它没有经过测试,所以可能需要一些努力才能让它工作。我正在测试您的代码,但是参数是什么呢?@hugohilario,这是一个
Sys.Mvc.ajaxContext
类型的对象,在MicrosoftMvcAjax.js
库中定义。它作为参数传递给AjaxOptions
客户端函数,并允许读取和操作ajax请求和响应。将其视为XMLHTTPROQUEST上的包装器
@model WebApplication1.Models.DummyModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
</head>
<body>
<div id="Parent">
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "post", OnBegin = "BeginClient" }))
{
@Html.AntiForgeryToken();
<div>First Name</div><div>@Html.TextAreaFor(m => m.FirstName)</div>
<div>Last Name</div><div>@Html.TextAreaFor(m => m.LastName)</div>
<input type="submit" value="Submit" />
}
</div>
<script type="text/javascript">
function BeginClient(xhr) {
alert("posting...");
securityToken = $('[name=__RequestVerificationToken]').val();
xhr.setRequestHeader('__RequestVerificationToken', securityToken);
}
$.ajaxSetup({
'beforeSend': function (xhr) {
securityToken = $('[name=__RequestVerificationToken]').val();
alert(securityToken);
xhr.setRequestHeader("__RequestVerificationToken", securityToken);
}
});
</script>
</body>
</html>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ValidateAntiForgeryTokenOnAllPostsAttribute : AuthorizeAttribute
{
/// <summary>
/// Executes authorization based on anti-forge token.
/// </summary>
/// <param name="filterContext">MVC pipeline filter context.</param>
public override void OnAuthorization(AuthorizationContext filterContext)
{
var request = filterContext.HttpContext.Request;
// Only validate POSTs
if (request.HttpMethod == WebRequestMethods.Http.Post)
{
// Ajax POSTs and normal form posts have to be treated differently when it comes to validating the AntiForgeryToken
if (request.IsAjaxRequest())
{
var antiForgeryCookie = request.Cookies[AntiForgeryConfig.CookieName];
var cookieValue = antiForgeryCookie != null
? antiForgeryCookie.Value
: null;
AntiForgery.Validate(cookieValue, request.Headers["__RequestVerificationToken"]);
}
else
{
new ValidateAntiForgeryTokenAttribute().OnAuthorization(filterContext);
}
}
}
}