前阿贾克斯后用不显眼的验证 [英] Validating using unobtrusive before ajax post
问题描述
所以,我一直在玩与周围的防伪造令牌,使得感谢进度到你们。
So I have been playing around with the Anti Forgery Token, making progress thanks to you guys.
我已经找到了一个解决方案,以合并的形式值ANF让我ActionMethods不上防伪标记吐......我在这个过程中不幸折断验证。客户端验证/客户端验证之前阿贾克斯后大火被忽略。服务器端的工作,但是我会挖掘后之前的一些验证。这里是code我使用。
I have figured out a solution to merge form values anf get my ActionMethods to not puke on the AntiForgery token... I have unfortunately broken validation in the process. The AJAX post fires before client side validation / client side validation is ignored. Server side works however I would dig some validation before the post.. Here is the code I am using.
$(document).ready(function () {
$('input[type=submit]').live("click", function (event) {
event.preventDefault();
// Form with the AntiForgeryToken in it
var _tokenForm = $(this).parents().find("#__AjaxAntiForgeryForm");
// Current Form we are using
var _currentForm = $(this).closest('form');
// Element to update passed in from AjaxOptions
var _updateElement = $(_currentForm).attr("data-ajax-update");
// Serialize the array
var arr = $(_currentForm).serializeArray();
//Merge TokenForm with the CurrentForm
$.merge(arr, $(_tokenForm).serializeArray());
// The AJAX Form Post stuff
$.ajax({
type: "POST",
url: $(_currentForm).attr('action'),
data: arr,
success: function (data) {
$(_updateElement).html(data);
}
});
return false;
});
});
所以,我想我需要的$就咕之前的一些方法来处理客户端验证...任何建议,将可能节省了一些时间。
So I am thinking that I need to handle the client side validation some way before the $.ajax goo... Any suggestions would possibly save me some time.
推荐答案
调用此:
var formValid = $("#FormId").validate().form();
if (!formValid) return false;
添加到您的code:
Added into your code:
$(document).ready(function () {
$('input[type=submit]').live("click", function (event) {
event.preventDefault();
// Form with the AntiForgeryToken in it
var _tokenForm = $(this).parents().find("#__AjaxAntiForgeryForm");
// Current Form we are using
var _currentForm = $(this).closest('form');
var isValid = $(_currentForm).validate().form();
if (!isValid) return false;
// Element to update passed in from AjaxOptions
var _updateElement = $(_currentForm).attr("data-ajax-update");
// Serialize the array
var arr = $(_currentForm).serializeArray();
//Merge TokenForm with the CurrentForm
$.merge(arr, $(_tokenForm).serializeArray());
// The AJAX Form Post stuff
$.ajax({
type: "POST",
url: $(_currentForm).attr('action'),
data: arr,
success: function (data) {
$(_updateElement).html(data);
}
});
return false;
});
});
这篇关于前阿贾克斯后用不显眼的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!