反伪造令牌和Ajax JSON Post不起作用

反伪造令牌和Ajax JSON Post不起作用,ajax,json,asp.net-mvc-3,antiforgerytoken,Ajax,Json,Asp.net Mvc 3,Antiforgerytoken,我正在运行MVC3、.NET4和VS2010。我有下面的示例项目来说明这个问题 我的控制器代码 namespace AntiForgeAjaxTest.Controllers { public class IndexController : Controller { public ActionResult Index() { MyData d = new MyData(); d.Age = 20;

我正在运行MVC3、.NET4和VS2010。我有下面的示例项目来说明这个问题

我的控制器代码

namespace AntiForgeAjaxTest.Controllers
{
    public class IndexController : Controller
    {
        public ActionResult Index()
        {
            MyData d = new MyData();
            d.Age = 20;
            d.Name = "Dummy";
            return View(d);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(MyData data)
        {
            NameValueCollection nc = Request.Form;
            return View(data);
        }

        protected override void ExecuteCore()
        {
            base.ExecuteCore();
        }
    }
}
我的视图和JavaScript代码

@model AntiForgeAjaxTest.Models.MyData

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
    <script src="../../Scripts/json2.js" type="text/javascript"></script>
</head>
<body>
@using (Html.BeginForm("Index", "Index"))
{ 
    @Html.AntiForgeryToken()

    <table>
        <tr>
            <td>Age</td>
            <td>@Html.TextBoxFor(x => x.Age)</td>
        </tr>
        <tr>
            <td>Name</td>
            <td>@Html.TextBoxFor(x => x.Name)</td>
        </tr>
    </table>

    <input type="submit" value="Submit Form" /> <input type="button" id="myButton" name="myButton" value="Ajax Call" />
}

<script type="text/javascript">
    $(document).ready(function () {

        $('#myButton').click(function () {
            var myObject = {
                __RequestVerificationToken: $('input[name=__RequestVerificationToken]').val(),
                Age: $('#Age').val(),
                Name: $('#Name').val(),
            };

            alert(JSON.stringify(myObject));

            $.ajax({
                type: 'POST',
                url: '/Index/Index',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(myObject),
                success: function (result) {
                    alert(result);
                },
                error: function (request, error) {
                    alert(error);
                }
            });
        });
    });
</script>
</body>
</html>
@model antiforgeaaxtest.Models.MyData
@{
布局=空;
}
指数
@使用(Html.BeginForm(“Index”,“Index”))
{ 
@Html.AntiForgeryToken()
年龄
@Html.TextBoxFor(x=>x.Age)
名称
@Html.TextBoxFor(x=>x.Name)
}
$(文档).ready(函数(){
$('#myButton')。单击(函数(){
变量myObject={
__RequestVerificationToken:$('input[name=\uu RequestVerificationToken]')。val(),
年龄:$(“#年龄”).val(),
名称:$('#Name').val(),
};
警报(JSON.stringify(myObject));
$.ajax({
键入:“POST”,
url:“/Index/Index”,
数据类型:“json”,
contentType:'application/json;charset=utf-8',
数据:JSON.stringify(myObject),
成功:功能(结果){
警报(结果);
},
错误:函数(请求、错误){
警报(错误);
}
});
});
});
这里我有两个按钮,第一个触发表单post,第二个触发Ajax post。表单post可以正常工作,但Ajax不能正常工作,服务器抱怨
未提供所需的防伪令牌或该令牌无效。
即使我已经在JSON中包含了该令牌

知道我的代码有什么问题吗?

这段代码很有效

@model AntiForgeAjaxTest.Models.MyData

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
    <script src="../../Scripts/json2.js" type="text/javascript"></script>
</head>
<body>
@using (Html.BeginForm("Index", "Index"))
{ 
    @Html.AntiForgeryToken()

    <table>
        <tr>
            <td>Age</td>
            <td>@Html.TextBoxFor(x => x.Age)</td>
        </tr>
        <tr>
            <td>Name</td>
            <td>@Html.TextBoxFor(x => x.Name)</td>
        </tr>
    </table>

    <input type="submit" value="Submit Form" /> <input type="button" id="myButton" name="myButton" value="Ajax Call" />
}

<script type="text/javascript">
    $(document).ready(function () {

        $('#myButton').click(function () {
            post();
        });
    });

    function post() {
        var myObject = {
            __RequestVerificationToken: $('input[name=__RequestVerificationToken]').val(),
            Age: $('#Age').val(),
            Name: $('#Name').val(),
        };

        $.post('/Index/Index/', myObject);
    }

</script>
</body>
</html>
@model antiforgeaaxtest.Models.MyData
@{
布局=空;
}
指数
@使用(Html.BeginForm(“Index”,“Index”))
{ 
@Html.AntiForgeryToken()
年龄
@Html.TextBoxFor(x=>x.Age)
名称
@Html.TextBoxFor(x=>x.Name)
}
$(文档).ready(函数(){
$('#myButton')。单击(函数(){
post();
});
});
职能职位(){
变量myObject={
__RequestVerificationToken:$('input[name=\uu RequestVerificationToken]')。val(),
年龄:$(“#年龄”).val(),
名称:$('#Name').val(),
};
$.post('/Index/Index/',myObject);
}

@nemesv我看过这篇文章,我也问了这个问题,但还没有答案。那么这个答案对你来说还不够吗?我不知道这有什么不同。post只是$.ajax的一个快捷方式,因此数据是相同的。唯一的区别是默认的内容类型是application/x-www-form-urlencoded;如果没有指定,charset=UTF-8,那么我回到了原点。如果要发布对象模型,这仍然不起作用。我开始怀疑这是否可以做到。