The required form field for counterfeiting "__RequestVerificationToken" is missing

here is another obstacle for me, I want my site to remain cross-site attacks, I am developing a Master / Detail form using asp.net mvc 5 through an Ajax request. So, to create one entry, I have to go through the Ajax request process, as follows:

$.ajax({
        url: '/Sales/Create',
        data: JSON.stringify(salesmain),
        type: 'POST',
        contentType: 'application/json;',
        dataType: 'json',
        success: function (result) {

            if (result.Success == "1") {
                window.location.href = "/Sales/index";
            }
            else {
                alert(result.ex);
            }
        }
    });

now it does not move to the β€œCreate” action in the sales controller, as the ajax request says, and before that it throws the following exception:

The required form field for counterfeiting "__RequestVerificationToken" is missing.

google, , , , __RequestVerificationToken jquery , JSON.stringify(salesmain) , :

$.ajax({
.
.
addRequestVerificationToken(JSON.stringify(salesmain))

:

function addRequestVerificationToken(data) {
data.__RequestVerificationToken = $('input[name=__RequestVerificationToken]').val();
return data;
};

, :

@using (Html.BeginForm())
{
  @Html.AntiForgeryToken()
  .
  .

:

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Create([Bind(Include = "SalesId,ReferenceNo,SalesDate,SalesPerson")] SalesMain salesMain)
{
.
.

jQuery 1.5, , , , , ? HELP , Advance:)

+4
1

addRequestVerificationToken() , ( javascript, data.__RequestVerificationToken = $(...) ).

,

data: JSON.stringify(addRequestVerificationToken(salesmain)),

, . contentType: 'application/json;',, application/x-www-form-urlencoded; charset=UTF-8'

data: addRequestVerificationToken(salesmain),

, HtmlHelper, (name="SalesId", name="ReferenceNo" ..),

data: $('form').serialize(),`

, .

+9

Source: https://habr.com/ru/post/1628834/


All Articles