I have a controller method
[HttpPost]
public ActionResult GetUserData()
{
    return Json(GetCurrentUser());
}
I'm calling it $.ajax() through a method like this:
ServerCall: function (method, args, callback) {
        $.ajax({
            type: 'POST',
            url: method,
            data: JSON.stringify(args),
            contentType: 'application/json;charset=utf8',
            dataType: 'json',
            success: function (result) {
                if (callback) {
                    callback(result);
                }
            },
            error: function (err) {
            }
        });
    }
with the call being:
ServerCall('GetUserData', null, function(data){
});
As it is, when I make this call, $.ajax returns with success, but 'data' is null. Debugging, responseText is empty. On the server side, GetUserData is called, and it is returning a properly formatted Json object (I've gone so far as to create my own JSON ActionResult and verified that data is indeed being written to the response stream.
If I add a dummy parameter to the server side method:
[HttpPost]
public ActionResult GetUserData(string temp)
{
    return Json(GetCurrentUser));
}
everything works perfectly. Browser is IE8. My question is, can anyone explain why this is happening?
UPDATE:
Note workaround solution below: I'd still be interested in knowing the root cause.
No repro.
Controller:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult GetUserData()
    {
        return Json(new { foo = "bar" });
    }
}
Index.cshtml view:
<script type="text/javascript">
    var serverCall = function (method, args, callback) {
        $.ajax({
            type: 'POST',
            url: method,
            data: JSON.stringify(args),
            contentType: 'application/json;charset=utf8',
            dataType: 'json',
            success: function (result) {
                if (callback) {
                    callback(result);
                }
            },
            error: function (err) {
            }
        });
    };
    serverCall('@Url.Action("GetUserData")', null, function (data) {
        alert(data.foo);
    });
</script>
result: 'bar' is alerted (as expected).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With