Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12367

An object with property values of null is received when Jquery.post is used

After switchng to ASP.NET MVC I'd never needed to send a JSON object to controller action, so I was comfortably using jqeury.post for my AJAX calls. All of the action methods were just receiving primitive types. But recently I had to send a JSON object, so I did this:

$.post(myUrl,JSON.stringify({param1:myJsonObject}),success(data){...},'json');

When I put a breakpoint to the beginning of the action method, to my surprise, I find all the properties of the received object to be null. However, if I don't "JSON.stringify" the object before sending, I can get the string properties correctly, but those with DateTime type are always 01.01.0001..

Now, if I try, so to say, old fashion way, everything goes just fine:

        $.ajax({
        url: myUrl,
        type: 'POST',
        data: JSON.stringify({param1:myJsonObject}),
        contentType: 'application/json',
        dataType: 'json',
        success: function (data) {
           //any code
        }
    });

All the properties are set to correct values. I've looked at the source code of the jquery.post and realized that the contentType was not set, so I set it to be application/json. But that didn't help either. The jquery version is 1.8.2. Do you have any idea what's wrong?

Upvotes: 0

Views: 665

Answers (1)

vanarajcs
vanarajcs

Reputation: 978

Try this

$.post(myUrl,JSON.stringify({param1:myJsonObject})).success(function(data){

 },'json');

Upvotes: 3

Related Questions