Skip to content Skip to sidebar Skip to footer

JQuery AJAX Post To MVC Controller Object -- Request Shows Up Null

I know I'm missing something in the details here. Problem Despite Googling, trying examples, different formats, etc, the AJAX request that I send always is validated as having all

Solution 1:

When you JSON.stringifyied your data object you converted it to JSON. But you forgot to set the Content-Type request header and the Web API has no way of knowing whether you are sending JSON, XML or something else:

$.ajax({
    url: '/api/contactus/newmessage',
    type: 'POST',
    contentType: 'application/json',
    done: submissionSucceeded,
    fail: submissionFailed,
    data: dataObject
});

Also when building the JSON you don't need to wrap it in an additional property that matches your method argument name. The following should work as well:

var dataObject = JSON.stringify({
    'Email': $('#inpEmail').val(),
    'Name': $('#inpName').val(),
    'PhoneNumber': $('#inpPhone').val(),
    'Message': $('#inpMessage').val()
});

Post a Comment for "JQuery AJAX Post To MVC Controller Object -- Request Shows Up Null"