$.post()
및 내 mvc 컨트롤러에 문제가 있습니다.Jquery Ajax 및 asp.net mvc 컨트롤러
나는이 같은 웹 및 스택 오버 플로우에 몇 가지 예를 시도 :
하지만 성공하지 How can I post an array of string to ASP.NET MVC Controller without a form?. 난 그저 내 컨트롤러에 문자열의 배열을 전달하려는 :
는[HttpPost]
public HttpResponseMessage Post([FromBody]List<string> idList, string request, bool auto)
{
LogHelper.Log(Severity.Info, "Just entered in POST api/files");
var fileList = new List<string>();
switch (request)
{
case "Activate":
fileList = auto ? _bendingsServies.GetBendigsToCome() : idList;
_filesServices.Activate(fileList);
break;
case "Archive":
fileList = auto ? _filesServices.GetFilesToArchive() : idList;
_filesServices.Archive(fileList);
break;
default:
return ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest);
break;
}
return ControllerContext.Request.CreateResponse(HttpStatusCode.OK);
}
내 JQuery와 : 나는 그것을하려고 할 때 시도 모든 것을
@using System.Configuration
var api_url = "@ConfigurationManager.AppSettings["apiUrl"]";
$(document).ready(function() {
// ---- Ajax calls ---- //
$("#archivebutton").click(function() {
var url = api_url + "Files?request=Archive&auto=false";
var fileList = ["10", "14", "12"];
var postData = { idList: fileList }
$.post(url, $.param(postData,true));
});
});
의 idList
는 항상 0 요소가 포함되어 있습니다.
// ---- 편집 ---- // 내가 JSON 개체에 캡슐화하는 경우 데이터를 보낼 수 있어요
:
$("#activatebutton").click(function() {
var url = api_url + "Files?request=Activate&auto=false";
var fileList = ["this", "is", "a", "test"];
$.post(url, { fileList: fileList});
});
이 요청입니다 :
fileList를 postData로 래핑하지 않고 직접 전달하면 어떻게됩니까? $. post (url, $ .param (fileList, true)); – swestfall
"* shallow *"컬렉션에'traditional'을 지정하지 않고 시도하십시오. 단지'$ .param (postData)'. –
싫어 ... 아직 다른쪽에 아무것도. – AntoineLev