【发布时间】:2017-08-13 03:39:05
【问题描述】:
我需要向 WebApi 发送一个字符串数组。我尝试了以下代码,但没有得到预期的输出。
在 Javascript 中:
$.ajax({
url:serviceUrl+'/sample',
type:'POST',
data:['a','b','c'],
contentType:'application/json',
dataType:'JSON'
}).done(function(data){
console.log(data);
}).fail(function(data){
console.log(data);
});
在控制器中:
[Route("sample")]
[HttpPost]
[ResponseType(typeof(string))]
public IHttpActionResult GetSample(List<string> dataFromUI)
{
return Ok("Success");
}
在 WebAPI 中,我得到没有元素的 dataFromUI。 我什至尝试使用 JSON.stringify 发送数据,但这次我的 dataFromUI 为 null。
如果我得到答案,我会很高兴。
谢谢。
【问题讨论】:
标签: javascript c# ajax asp.net-web-api http-post