NULL controller parameter when using jQuery POST and ASP.NET MVC

I have no problem processing jQuery GET requests in the controller, however I cannot get the form data in POST. Customer Fragment

$.post(url,{name:"John"},function(result){ 
    //process result
});

in combination with a controller,

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

will result in a NULL value for the name parameter when checking inside the action method, whereas I expected the name to be mapped to the method parameter. Also all other objects (Request.Form), etc. In this context, it seems NULL. I can do this with help $.get, but I think I should perform any side effect operations with POST. I am using ASP.NET MVC 1.0, jQuery 1.2.6 and Internet Explorer 7.

Thanks!

Update : see my answer below and a modest apology

+3
4

, , $.ajaxSetup , contentType application/json.

contentType :

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

, processData , , JSON (: "name = John" ).

, :) JSON, , .

+6

, , URL- ? , Fiddler, , , .

+2

, Save (string name) JSON? :

$.post(url,
"{'name':'John'}", function(result){
});
+1

, json .

. , JSON , .

+1

Source: https://habr.com/ru/post/1708090/


All Articles