Monday, November 26, 2012

Automatically map XmlhttpRequest post data to MVC action partameters

After converting a html Form to a xmlHttpRequest post request, the MVC action method can no longer get the mapped parameter value sent from client. The post data string is the exact between the two cases.

It turns out it is caused by not setting the Content-Type header in xmlhttprequest  object, and it is reasonable that server action method will not try to map the parameter value from post data, as with post request, the post data may be binary, json or any other formats.

So in order to fix the issue, the only required change is setting the content type explicitly for the request:

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

And the server action method just stars to work again.

No comments:

Post a Comment