2015-06-30 60 views
0

我想发布一个窗体到一个MVC控制器,采用AJAX的窗体集合。我一直在关注这个How to pass formcollection using ajax call to an action?。然而,当我向控制器发出发布请求时,它以某种方式反转路径的顺序,例如在我的AJAX代码我的网址是'/Settings/EditDatasource'但是当我做POST请求变得http://localhost:53658/EditDatasource/Settings表格不发布资源未找到

这里是我的AJAX代码

$(document).ready(function() { 
    $('#postEditDatasource').click(function (event) { 
     alert(JSON.stringify(deletedDatapoints)); 
     //serialise and assign json data to hidden field 
     $('#dsDeletedDP').val(JSON.stringify(deletedDatapoints)); 

     //anti forgery token 
     //get the form 
     var form = $('#__dsAjaxAntiForgeryForm'); 
     //from the form get the antiforgerytoken 
     var token = $('input[name="__RequestVerificationToken"]', form).val(); 

     var URL = 'Web/Settings/EditDatasource'; 

     //we make an ajax call to the controller on click 
     //because the controller has a AntiForgeryToken attribute 
     //we need to get the token from the form and pass it with the ajax call. 
     $.ajax({ 
      url: URL + form.serialize(), 
      data: { 
       __RequestVerificationToken: token, 
      }, 
      type: 'POST', 
      success: function (result) { 
       if (data.result == "Error") { 
        ShowDatasourcePostAlert('failPost', 3000); 
       } else { 
        ShowDatasourcePostAlert('successPost', 3000); 
       } 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       alert("An error has occurred please contact admin"); 
      } 
     }) 
    }); 
}) 

,这里是我的控制器:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult EditDatasource(FormCollection collection) 
    { 

     return new EmptyResult(); 
    } 
+0

快速思考 - 'form.serialize()'应该放在'Request Body'中,但我看到你传递的是URL。同时告诉我们您的控制器操作代码。 – ramiramilu

+0

@ramiramilu我的控制器没有做任何事情,除了返回一个空的结果,因为我不希望它做任何事后。我将发布自己的代码以及属性标记 – Johnathon64

+0

您是否曾尝试在URL中移除'form.serialize()'并将其传递给'data' - 'data:form.serialize()'。 – ramiramilu

回答

1

这里去了解决方案如下。创建一个简单的POST Action

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult MyIndex(FormCollection collection) 
{ 
    var fname = collection["FirstName"]; 
    var lname = collection["LastName"]; 
    return Json(true); 
} 

让你的HTML是 -

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "MyForm" })) 
{ 
    @Html.AntiForgeryToken() 
    @Html.TextBox("FirstName","Rami") 
    <input type="text" name="LastName" id="LastName" /> 
} 

<input type="button" value="Click" id="btnSub" /> 

<script type="text/javascript"> 
    $('#btnSub').click(function() {   
     var form = $('#MyForm'); 
     console.log(form); 
     $.ajax({ 
      url: '/Home/MyIndex/', 
      type: 'POST', 
      data: form.serialize(), 
      success: function (result) { 
       alert(result); 
      } 
     }); 
     return false; 
    }); 
</script> 

和输出将是 -

enter image description here

注: 如果你不使用@Html.AntiForgeryToken() ,那么ValidateAntiForgeryToken会抛出你的错误。因此,您无需在JQuery AJAX Post中明确传递AntiForgeryToken

+0

如果我有隐藏的字段呢?那么我该怎么做? – Johnathon64

+0

所有隐藏的字段都将被序列化并将发布到服务器,无需任何额外的工作。事实上,伪造令牌也是一个隐藏的领域。 – ramiramilu

+0

我会给出这个结果,让你知道结果 – Johnathon64