本文介绍了jQuery Ajax发布参数在控制器中为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ajax POST请求:
I have an ajax POST request:
$.ajax({
url: "/api/EmmaWeb/SaveFavourite", //window.location.pathname + "?handler=favourite", //"/it/Umbria/vocabolo-menicaglie-6?handler=favourite",
type: "POST",
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]', $form).val() },
//contentType: "application/jsonrequest; charset=utf-8",
//contentType: 'application/json',
//dataType: "json",
data: JSON.stringify({ isFavourite: $inputIsFavourite.val().toLowerCase(), propertyUnitSlug: $PropertyUnitSlug.val() }),
cache: false
}).done(function (resp) {
console.log("done", resp);
});
我的控制器:
[ApiController]
[Route("/api/[controller]")]
public class EmmaWebController : Controller
{
[HttpPost]
[Route("[action]")]
public async Task<IActionResult> SaveFavouriteAsync(bool isFavourite, string propertyUnitSlug)
{
// DO Something
return new JsonResult(new
{
// Something
});
}
}
我在控制器中执行操作,但是参数始终为null.我尝试添加[FromBody],但出现415错误
I reach my action in controller, but the parameters are always null. I tried adding [FromBody] but i get 415 error
推荐答案
我解决了从Controller中删除[ApiController]属性的问题,这就是ajax:
I Solved removing [ApiController] attribute from the Controller, and this is the ajax:
$.ajax({
url: "/api/EmmaWeb/SaveFavourite", //window.location.pathname + "?handler=favourite", //"/it/Umbria/vocabolo-menicaglie-6?handler=favourite",
type: "POST",
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]', $form).val() },
//contentType: "application/jsonrequest; charset=utf-8",
//dataType: "json",
//contentType: 'application/json',
//dataType: "json",
data: { "isFavourite": $inputIsFavourite.val(), "propertyUnitSlug": $PropertyUnitSlug.val() },
cache: false
})
这篇关于jQuery Ajax发布参数在控制器中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!