我想發佈一個窗體到一個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();
}
快速思考 - 'form.serialize()'應該放在'Request Body'中,但我看到你傳遞的是URL。同時告訴我們您的控制器操作代碼。 – ramiramilu
@ramiramilu我的控制器沒有做任何事情,除了返回一個空的結果,因爲我不希望它做任何事後。我將發佈自己的代碼以及屬性標記 – Johnathon64
您是否曾嘗試在URL中移除'form.serialize()'並將其傳遞給'data' - 'data:form.serialize()'。 – ramiramilu