Search code examples
ajax.net-corepostman

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


Solution

  • 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);
            }
    
        });