2015-06-30 59 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