简体   繁体   English

当FromBody MVC Core2.2剂量工作时出现错误请求

[英]Bad Request whenFromBody mvc core2.2 dosent work

fromBody does not work in Web mvc Core 2.2 for Post In Postman I am trying to post some data. fromBody在Web mvc Core 2.2中无法用于Post In Postman我正在尝试发布一些数据。 I have made it as simple as possible; 我已经使它尽可能简单。

iam see error 400 Bad Request iam看到错误400错误的请求

  var UserVmodl =
          {
             In_User_Name:'',
             In_Family :$("#Family").val(),
             User_Name :$("#Name").val()
             In_National_Code :$("#National_Code").val(),
             In_Birth_Date_sh :$("#dateBirth_Date").val(),
              In_Email:$("#Email").val()
          }
  $.ajax({
            url: '@Url.Action("SaveUser", "PersonalInfo")',
            type: 'POST',
            data: JSON.stringify(UserVmodl ),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
             async: true,
            beforeSend: function (request) {
                sendRequestVerificationToken(request);
            },
            success: function (data) { localSuccess(data); onSuccessFunc(data); }
        });

[Route("PersonalInfo")]
  [Area("client")]
  public class PersonalInfoController : Controller
  {

   [HttpPost]
    [Route("save")]
    public  IActionResult SaveUser([FromBody] PersonalInfo UserVmodl)
    {
      //var Result= _IUserService.UpdateUser(UserVmodl);
      return View();
    }
}

Try to send RequestVerificationToken from headers if you need yo add antiforgery validation. 如果您需要添加防伪验证,请尝试从标头发送RequestVerificationToken。

 $.ajax({
                url: '@Url.Action("SaveUser", "PersonalInfo")',
                type: 'POST',
                headers : {
                  RequestVerificationToken:
                      $('input:hidden[name="__RequestVerificationToken"]').val()
                },                   
                data: JSON.stringify(UserVmodl),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                async: true,
                success: function (data) { localSuccess(data); onSuccessFunc(data); }
            });

Remember to add @Html.AntiForgeryToken() in your form. 请记住在表单中添加@Html.AntiForgeryToken() Besides,you need to return a json result since your ajax needs a return type of json. 此外,由于您的ajax需要json的返回类型,因此您需要返回json结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 core2.2 mvc 项目中使用 IClaimsTransformation - how to use IClaimsTransformation in core2.2 mvc project .Net Core2.2:如何在EF Codefirst方法中更改字符集 - .Net Core2.2 : how to change characterset in EF codefirst approach 如何在ASP.Net Core2.2中覆盖415响应 - How to Override 415 response in ASP.Net Core2.2 如何使用 articleName=sample-article-name [MVC Core2.2] 将“www.example.com/sample-article-name”路由到 RazorPage Index.cshtmll - How to route to "www.example.com/sample-article-name" to RazorPage Index.cshtmll with articleName=sample-article-name [MVC Core2.2] 使用 ASP.NET Core2.2 中的 CancellationToken 取消内部任务 - Cancel an internal task using CancellationToken in ASP.NET Core2.2 使用HttpClient Alwyes Null ASP.net Core2.2将IFormFile发送到API - Send IFormFile To API using HttpClient Alwyes Null ASP.net Core2.2 无法使用 dinktopdf 为 .net core2.2 azure functionapp 加载 dll libwkhtmltox - Unable to load the dll libwkhtmltox using dinktopdf for a .net core2.2 azure functionapp EF Core2.2:Scaffold-DbContext无法在WPF项目中使用命名连接字符串 - EF Core2.2: Scaffold-DbContext not working with named connection string in WPF project 创建模型时不能使用上下文。 EF-核心ASP.net Core2.2 - The context cannot be used while the model is being created. EF-Core ASP.net Core2.2 无法使用 Firefox 调试某些 asp.net core2.2 网页。 如何使用 Firefox 进行调试? - Unable to debug some asp.net core2.2 web pages with Firefox. How can I debug with Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM