2012-06-21 62 views
-1

我正在运行MVC3,.Net 4和VS2010。我有以下示例项目来说明问题。防伪令牌和Ajax JSON帖子不起作用

我的控制器代码

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> 

这里我有2个按钮,第一个触发器的形式后,第二个触发的Ajax交。表单邮件正常工作,但Ajax邮件没有,服务器抱怨A required anti-forgery token was not supplied or was invalid.,即使我已经在我的JSON中包含令牌。

任何想法我的代码有什么问题?

+0

@nemesv我看到这篇文章,我也问过那里的问题,但没有答案。 – hardywang

+0

那么这个答案对于你来说还不够吗? http://stackoverflow.com/a/7603172/538387 – Tohid

回答

1

此代码有效。

@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> 
+0

我很困惑这是什么不同。 $ .post只是$ .ajax的一个快捷方式,所以数据是一样的。唯一的区别是默认的内容类型是application/x-www-form-urlencoded;如果没有指定,charset = UTF-8,所以我回到了原来的位置。如果您发布对象模型,这仍然不起作用。我开始想知道这是否可以完成。 – iGanja