Ajax 请求 Post 参数

Ajax request Post Params

我想使用 ajax post 请求,但我的请求仅在我使用 Get 请求时有效。

当我使用获取请求时,我的模型被正确填充。对于 post 请求,我的模型为空。

所以这有效:

self.ClearRegimeFromUntil = function () {
    var model = { clearFrom: kendo.format("{0:dd/MM/yyyy hh:mm:ss tt}", self.clearFrom()), clearUntil: kendo.format("{0:dd/MM/yyyy hh:mm:ss tt}", self.clearUntil()), userId: self.UserID() };
    console.log(model);
    $.ajax({
        type: "GET",
        url: rootUrl + "api/CalendarDays/ClearRegimeFromUntil",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: model,
        success: function (response) {
            console.log(response);

        }
    });
};

在控制器端:

    [System.Web.Http.HttpGet]
    [System.Web.Http.Route(@"api/CalendarDays/ClearRegimeFromUntil")]
    public async Task<IHttpActionResult> ClearRegimeFromUntil([FromUri] DeleteFromModel model)
    {

        //await _userCalendarDayRepository.DeleteFutureUserCalendarDays(model.UserId,DateTime.Parse( model.ClearFrom));
        return Ok(true);
    }

但这行不通:

self.ClearRegimeFromUntil = function () {
    var model = { clearFrom: kendo.format("{0:dd/MM/yyyy hh:mm:ss tt}", self.clearFrom()), clearUntil: kendo.format("{0:dd/MM/yyyy hh:mm:ss tt}", self.clearUntil()), userId: self.UserID() };
    console.log(model);
    $.ajax({
        type: "POST",
        url: rootUrl + "api/CalendarDays/ClearRegimeFromUntil",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: model,
        success: function (response) {
            console.log(response);

        }
    });
};

在控制器端:

    [System.Web.Http.HttpPost]
    [System.Web.Http.Route(@"api/CalendarDays/ClearRegimeFromUntil")]
    public async Task<IHttpActionResult> ClearRegimeFromUntil([FromUri] DeleteFromModel model)
    {

        //await _userCalendarDayRepository.DeleteFutureUserCalendarDays(model.UserId,DateTime.Parse( model.ClearFrom));
        return Ok(true);
    }

我的模特:

public class DeleteFromModel
{
    public string UserId { get; set; }
    public string ClearFrom { get; set; }
    public string ClearUntil { get; set; }

}

Headers 和回复(不知道如何更好地展示它们): http://pastebin.com/AM6ScEsz

检查您的 html 表格是否在 POST

<form method="post">
...
</form>

能否将您的网络 headers 和 ajax 请求的响应发送给我们?

删除 [FromUri] 将解决问题。

[System.Web.Http.HttpPost]
[System.Web.Http.Route(@"api/CalendarDays/ClearRegimeFromUntil")]
public async Task<IHttpActionResult> ClearRegimeFromUntil(DeleteFromModel model)
{

    //await _userCalendarDayRepository.DeleteFutureUserCalendarDays(model.UserId,DateTime.Parse( model.ClearFrom));
    return Ok(true);
}