简体   繁体   中英

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. I have made it as simple as possible;

iam see error 400 Bad Request

  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.

 $.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. Besides,you need to return a json result since your ajax needs a return type of json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM