3

I have no problems processing jQuery GET requests in a controller, however I cannot get any form data to POST. The client snippet

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

combined 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 inspected inside the action method, whereas I expected name to be mapped to the method parameter. Also all other objects (Request.Form), etc. in this context seem to be NULL. I can do this with a $.get, but I think I am supposed to do any operations with side-effects with POSTs. I am using ASP.NET MVC 1.0, jQuery 1.2.6 and Internet Explorer 7.

Thanks!

Update: see my answer below and humble apologies

4 Answers 4

6

Sorry guys, I had a $.ajaxSetup entry in the page which overrided the default contentType to application/json.

When using the default contentType as follows:

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

It works because processData is true by default which means the data entry with the JSON object will be parsed into a string (data: "name=John" also works).

Sorry for wasting your time :) and thanks to Mark for the suggestion on passing JSON objects, ill do that next cause it seems very cool.

1
  • 1
    Spent an hour on this. Thanks answering your own question. ;) Commented Dec 25, 2012 at 8:50
2

I believe your code should work, is your URL correct and your routes setup correctly? In addition you could always fire up Fiddler to see exactly what your request to the server is and if you are passing the correct items.

1
  • Thanks! ill try Fiddler. Ive used Web Development Helper in IE and it does seem to pass the right request body to the method. Commented May 11, 2009 at 15:31
1

Could it be that the Save(string name) method is expecting stringified JSON? Try this:

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

It isn't so simple as making a json object and throwing it at an action.

Start from here. People have written small scripts that get the JSON object dressed and ready for an action to read it in and map it to properties or arguments.

2
  • I'll read this; it seems though the link addresses how to pass JSON to action methods that expect complex custom types as parameters -which i'd like to do next!, but for now im simply trying to pass a string, which it seems should be straighforward - Thanks Commented May 11, 2009 at 15:28
  • 1
    Yeah, that's true. What is nice about passing JSON objects in is that you can write cool action filters to deserialize, and validate them. It's very good for AJAX with MVC. Hope it helps, thanks for your comment. Commented May 11, 2009 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.