gpt4 book ai didi

javascript - Ajax请求Post参数

转载 作者:行者123 更新时间:2023-12-03 07:18:04 25 4
gpt4 key购买 nike

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

当我使用获取请求时,我的模型已正确填充。通过发布请求,我的模型为空。

所以这有效:

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);

}
});
};

在 Controller 端:

    [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);

}
});
};

在 Controller 端:

    [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; }

}

标题和响应(不知道如何更好地显示它们): http://pastebin.com/AM6ScEsz

最佳答案

删除 [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);
}

关于javascript - Ajax请求Post参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com