views:

194

answers:

1

I have a problem with a simple VS2010 .NET 4.0 MVC2 application.

My controller action looks like this:

public JsonResult GetJson(string query)

I access the action with jQuery like so:

function getJson() {
var postdata = {};
postdata['query'] = $('#query').val();
$.ajax({
type: "POST",
url: '<%= Url.Action("GetJson") %>',
data: JSON.stringify(postdata),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {

The action is executed upon the jQuery XHR request, but no matter what the "query" value is ALWAYS null. I can view the POST request/response in Firebug, and it shows the proper JSON string being sent to the action.

What could the problem be? It just seems like MVC is not parsing/deserializing the JSON input at all.

Thanks!

A: 

Please try this as it should work for you:

$.ajax({
    type: "POST",
    url: '<%= Url.Action("GetJson") %>',
    data: {query: $('#query').val()},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
    }
});
Khnle