我想发布一个窗体到一个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