Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

ASP.NET AJAX Returns Null even with a [FromBody] attribute

$
0
0

Dear Sirs.

Please Help. I'm new in Ajax what I'm doing wrong.

Do I need some action on startup.cs ? or ..

Here is a Controller Code

        [HttpPost]

        public IActionResult MyMethod([FromBody] int paramint, [FromBody] string filename)

        {

            if (filename != null)

                return (Json(true));

            return (Json(false));

        }

and JS Code

function Test() {

    

    $.ajax({

        type: "POST",

        headers: {

            "Accept": "application/json",

            "Content-Type": "application/json"

        },

        dataType: "json",

        url: '/Home/MyMethod',

        data: 

        {

            "paramint": 2,

            "filename" : "file.txt"

        }

        ,

        error: function () {

            alert("Cant Access at all");

        },

        success: function (operationState) {

            if (operationState === true) {

                alert("Finaly :)");

            }

            else {

                alert("Still not set the parameters");

            }

        }

    });

}

 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>