使用jQuery POST和ASP.NET MVC时,Controller参数为NULL

mar*_*elf 3 asp.net-mvc jquery

我在控制器中处理jQuery GET请求没有问题,但我无法获取任何表单数据到POST.客户端代码段

$.post(url,{name:"John"},function(result){ 
    //process result
});
Run Code Online (Sandbox Code Playgroud)

结合控制器,

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(string name)
{
    return Json("Success!");
}
Run Code Online (Sandbox Code Playgroud)

在action方法中检查时,将导致name参数的NULL值,而我希望将name映射到method参数.此外,此上下文中的所有其他对象(Request.Form)等似乎都是NULL.我可以用a做这个$.get,但我想我应该做任何带POST的副作用的操作.我正在使用ASP.NET MVC 1.0,jQuery 1.2.6和Internet Explorer 7.

谢谢!

更新:请参阅下面的答案和谦虚的道歉

mar*_*elf 6

对不起,伙计,我在页面中有一个$ .ajaxSetup条目,它将默认的contentType覆盖到application/json.

使用默认contentType时如下:

$.ajax({ url,
         type: "POST",
         contentType: "application/x-www-form-urlencoded",
         success: function(result) { alert(result); },
         data: { name: "John" }
        });
Run Code Online (Sandbox Code Playgroud)

它的工作原理是因为默认情况下processData为true,这意味着带有JSON对象的数据条目将被解析为一个字符串(数据:"name = John"也可以).

很抱歉浪费你的时间:)并感谢Mark提供有关传递JSON对象的建议,接下来生病了,因为它看起来非常酷.