开发者

MVC2 JSON input

开发者 https://www.devze.com 2023-01-02 02:14 出处:网络
I have a problem with a simple VS2010 .NET 4.0 MVC2 application. My controller action looks like this:

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",

开发者_Go百科 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!


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) {
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消