0

我正在尝试将列表发布到我的 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}
    ]}

空值示例:

例子

我尝试了更多..有人可以帮我吗?谢谢!!!

编辑: 答案是我application/xml在需要发送时发送了内容类型application/json

4

3 回答 3

1

我试过这个 -

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        var foo = new List<FollowItem>()
        {
            new FollowItem {action = true, FollowsUserId = 123456777},
            new FollowItem {action = true, FollowsUserId = 123456888}
        };
        return new JsonResult {Data = foo, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
    }

    //
    // POST: /Home/
    public ActionResult Dump(List<FollowItem> followItems)
    {
        Debug.WriteLine(followItems);
        return new HttpStatusCodeResult(200);
    }

}
public class FollowItem
{
    public bool action;
    public long FollowsUserId;
}

并张贴这个 -

[
  {"action":true,"FollowsUserId":123456777},
  {"action":true,"FollowsUserId":123456888}
]

这有效。请注意,这也是它发送响应的方式。

于 2013-01-29T10:25:55.063 回答
1

尝试将变量名称添加到数据中。请参阅以下代码。

function postFollowList() {
        $.ajax( {
        url: Request_Url + "api/UserFollows",
        type: 'post',
        data: { followItemList: [
            {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);
        }
});

编辑:也许你可以在发布之前尝试序列化数组:

        data: { followItemList: JSON.stringify([
            {action: true, FollowsUserId: 123456777},
            {action: true, FollowsUserId: 123456888}
        ])},
于 2013-01-29T10:26:39.680 回答
1

JSON 似乎无效。也许试试这个:

[
    {
        "action": true,
        "FollowsUserId": 123456777
    },
    {
        "action": true,
        "FollowsUserId": 123456888
    }
]

检查 JSON 有效性的一个好工具是jsonlint.com

于 2013-01-29T10:22:35.817 回答