簡體   English   中英

Ajax使用反偽造令牌將JSON模型發布到ASP.Net MVC4

[英]Ajax post a JSON model to ASP.Net MVC4 with Anti-forgery token

我正在通過ajax發布提交json模型。 添加用戶驗證后,它不起作用。

    var token = $('input[name=""__RequestVerificationToken""]').val();
    var headers = {};
    headers['__RequestVerificationToken'] = token;

        $.ajax({
            url: '/SalesQuotation/Create',
            cache: false,
            headers: headers,
            data: JSON.stringify(salesquotation),
            type: 'POST',
            contentType: 'application/json;',
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.Success == "1") {
                   window.location.href = "/SalesQuotation/Create";
                }
                else {
                    alert(result.ex);
                }
            }
         });

控制器:

   [HttpPost]
   [ValidateAntiForgeryToken]
   public JsonResult Create(SalesQuotation salesquotation)
    {
        try
        {
            if (ModelState.IsValid)
            {
                if (salesquotation.QuotationId > 0)
                {

                    var CurrentsalesQuotationSUb = db.SalesQuotationSubs.Where(p => p.QuotationId == salesquotation.QuotationId);
                    foreach (SalesQuotationSub ss in CurrentsalesQuotationSUb)
                        db.SalesQuotationSubs.Remove(ss);

                    var CurrentsalesQuotationDta = db.DTATrans.Where(p => p.QuotationId == salesquotation.QuotationId);
                    foreach (DTATran ss in CurrentsalesQuotationDta)
                        db.DTATrans.Remove(ss);

                    foreach (SalesQuotationSub ss in salesquotation.salesquotationsubs)
                        db.SalesQuotationSubs.Add(ss);

                    foreach (DTATran ss in salesquotation.dtatrans)
                        db.DTATrans.Add(ss);

                    db.Entry(salesquotation).State = EntityState.Modified;
                }
                else
                {
                    db.SalesQuotations.Add(salesquotation);
                }

                db.SaveChanges();
            }
        }

        catch (Exception ex)
        {
            return Json(new { Success = 0, ex = "Unable to save... " + ex.Message.ToString()});
        }
       return Json(new { Success = 1, ex = new Exception("Saved successfully.").Message.ToString() });
    }

視圖:

@using (Html.BeginForm())
{

    @Html.ValidationSummary(true)
    <input name="__RequestVerificationToken" type="hidden"          
    value="H4zpQFvPdmEdGCLsFgeByj0xg+BODBjIMvtSl5anoNaOfX4V69Pt1OvnjIbZuYrpgzWxWHIjbng==" />

服務器返回

我的方法中可能缺少什么。 請指教....

屬性選擇器周圍應該只有一組引號。 您的代碼兩邊都有兩個引號。

這個:

var token = $('input[name=""__RequestVerificationToken""]').val();

應該是這樣的:

var token = $('input[name="__RequestVerificationToken"]').val();

在操作方法中使用[ValidateJsonAntiForgeryToken]屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM