【发布时间】:2013-01-12 20:58:15
【问题描述】:
我正在尝试将列表发布到我的 MVC 控制器..
控制者: // POST api/UserFollows
public HttpResponseMessage PostUserFollows(List<FollowItem> followItemList)
{
//I'M GETTING NULL IN followItemList
if(followItemList==null)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
输出:
STATUS 400 Bad Request <-- Means I got null in followItemList
TIME 4025 ms
Cache-Control →no-cache
Connection →Close
Content-Length →0
Date →Tue, 29 Jan 2013 09:38:31 GMT
Expires →-1
Pragma →no-cache
Server →ASP.NET Development Server/10.0.0.0
X-AspNet-Version →4.0.30319
FollowItem 类
namespace Helpers.SubClasses
{
public class FollowItem
{
public bool action;
public long FollowsUserId;
}
}
我尝试了很多请求,但没有一个有效..我总是得到空!
发布方法:
function postFollowList() {
$.ajax( {
url: Request_Url + "api/UserFollows",
type: 'post',
data: {
{action: true, FollowsUserId: 123456777},
{action: true, FollowsUserId: 123456888}
},
dataType: 'json',
success: function( data )
{
$('#newmovie').html("OK");
},
error: function (jqXHR, textStatus, err)
{
$('#newmovie').html('Error: ' + err);
}
});
请求: //作为 JSON - 我正在使用 POSTMAN
1.
[
{"action":"true","FollowsUserId":"123456777"}
]
2.
[
{action: true, FollowsUserId: 123456777},
{action: true, FollowsUserId: 123456888}
]
3.
{[
{action: true, FollowsUserId: 123456777},
{action: true, FollowsUserId: 123456888}
]}
4.
{followItemList:[
{action: true, FollowsUserId: 123456777},
{action: true, FollowsUserId: 123456888}
]}
null 示例:
我尝试了很多.. 有人可以帮我吗? 谢谢!!!
编辑:
答案是当我需要发送 application/json 时,我在 content-type 中发送了 application/xml。
【问题讨论】:
-
什么时候得到空值?您的代码在做什么?“我得到空值”是什么意思?
-
你是怎么发帖的?请显示完整代码。
-
@RoyDictus 添加了更多信息
-
@ken2k 添加了 Post 方法 谢谢!
标签: c# asp.net-mvc json entity-framework jquery