Home > Enterprise >  ajax post asp.net core 3.1 server side problme
ajax post asp.net core 3.1 server side problme

Time:07-01

my application running smoothly in local environment. When the size of the array posted on the server side increases, it gives an error of 400. (I don't have any problems when posting an array with 53 rows. An array with 566 rows is not posted). Ajax side, Action and error are given below. (I post to the same database in the local environment.) enter image description here

$("body").on("click", "#btnSubmitTest", function () {

Swal.fire({
    title: 'Test tamamlanacaktır. Onaylıyor musunuz ?',
    showCancelButton: true,
    confirmButtonText: 'Evet',
    cancelButtonText: `Hayır`,
    customClass: {
        confirmButton: "btn btn-primary",
        cancelButton: "btn btn-danger"
    }
}).then((result) => {
    if (result.isConfirmed) {
        var qn = document.getElementById("hiddenQNo").value;
        var Answers = new Array();
        var SendedTestId = document.getElementById("hiddenSendedTestId").value
        var isNull = 0;
        var nullArray = new Array();
        for (let i = 1; i <= qn; i  ) {
            var answerVal = table.$('#'   i ":checked").serialize();
            const myArray = answerVal.split("=");
            if (myArray[1] == undefined) {
                isNull = isNull   1;
                nullArray.push(i);
            }
            var answerModel;
            answerModel = ({
                TestOrder: i,
                Answer: myArray[1],
                SendedTestId: SendedTestId
            });
            Answers.push(answerModel);
            myArray.pop();
        }
        var Data = { 'TestAnswer': Answers }
        if (isNull == 0) {
            $.ajax({
                url: '/StartTest/SubmitTest',
                type: 'post',
                data: Data,
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("XSRF-TOKEN",
                        $('input:hidden[name="__RequestVerificationToken"]').val());
                },
                success: function (data) {
                    Swal.fire({
                        icon: 'success',
                        text: data.responseText,
                        showConfirmButton: false,
                    })
                    setTimeout(function () {
                        window.location.href = "StartTest/Completed"
                    }, 2000)
                }
            });
        }
            else {
            Swal.fire({
                icon: 'error',
                text: 'Tüm soruları cevaplamanız gerekmektedir! ' nullArray ' ' 'boş bırakılmıştır.',
                confirmButtonColor: '#3085d6'
            })
            }
    }
})

});

 [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> SubmitTest(List<TestAnswer> testAnswer)
    {
        try
        {
            if (testAnswer != null)
            {
                var UrlModel = _context.UrlModels.Where(x => x.SendedTestId == testAnswer[0].SendedTestId).FirstOrDefault();
                UrlModel.CompletedDate = DateTime.Now;
                _context.UrlModels.Attach(UrlModel);
                _context.SaveChanges();
                _context.TestAnswers.AddRange(testAnswer);
                _context.SaveChanges();

                return Json(new { success = true, responseText = "Sonuçlar kaydedildi!", id=UrlModel.SendedTestId });
            }
            else
            {
                return Json(new { success = false, responseText = "Bir Hata Oluştu." });
            }
        }
        catch (Exception)
        {

            return Json(new { success = false, responseText = "Bir Hata Oluştu." });
        }
       
    }

CodePudding user response:

try increasing size limit of your requests, add a webconfig to your project and add this code

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <security>
      <requestFiltering>
        <!-- This will handle requests up to 50MB -->
        <requestLimits maxAllowedContentLength="52428800" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
  • Related