Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core FromBody is null on AJAX POST but populates in Postman

I'm trying to perform an AJAX post but I keep getting a null FromBody in my .NET controller. I think it has to do with how I'm formatting my AJAX post.

When I attempt to post with AJAX I get a null FromBody.

    var data = {
    Date: "2016-12-01",
    BurnIdx: 23,
    BurnStatIdx1: 3,
    BurnStatIdx2: 3,
    BurnStatIdx3: 3,
    BurnSevIdx: 5,
    WorkOrder: 32426,
    Comment: "Hi"
};

$('#submit').on('click',function () {
    $.ajax({
        type: 'POST',
        url: 'Home/BurnerMapUpdate',
        dataType: 'json',
        contentType: 'application/json',
        data: data,
        success: function (result) {
            console.log('Data received');
            console.log(result);
        }

    });
});

Null FromBody

However, when I attempt a post in Postman it's successful.

Postman Not null

like image 796
Tony Martinez Avatar asked Oct 16 '25 15:10

Tony Martinez


2 Answers

Figured out my problem. Needed to use JSON.stringify on my data.

    $('#submit').on('click',function () {
    $.ajax({
        type: 'POST',
        url: 'Home/BurnerMapUpdate',
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: function (result) {
            console.log('Data received');
            console.log(result);
        }

    });
like image 101
Tony Martinez Avatar answered Oct 18 '25 08:10

Tony Martinez


Not only the JSON.stringify(). If your data don't match with csharp class data it doesn't receive anything. Like if you define in the class a field with int type, and send it in the json like "1", it happens to receive the whole data as null

like image 23
Fernando Oliveira Avatar answered Oct 18 '25 10:10

Fernando Oliveira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!