您错误地指定
contentType到
application/json。
这是一个可能如何工作的示例。
控制器:
public class HomeController : Controller{ public ActionResult Index() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Index(string somevalue) { return Json(new { somevalue = somevalue }); }}视图:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "__AjaxAntiForgeryForm" })){ @Html.AntiForgeryToken()}<div id="myDiv" data-url="@Url.Action("Index", "Home")"> Click me to send an AJAX request to a controller action decorated with the [ValidateAntiForgeryToken] attribute</div><script type="text/javascript"> $('#myDiv').submit(function () { var form = $('#__AjaxAntiForgeryForm'); var token = $('input[name="__RequestVerificationToken"]', form).val(); $.ajax({ url: $(this).data('url'), type: 'POST', data: { __RequestVerificationToken: token, somevalue: 'some value' }, success: function (result) { alert(result.somevalue); } }); return false; });</script>


