jQuery posts null instead of JSON to ASP.NET Web API

So I found the problem, and the solution. So, first thing first. The contentType cannot be ‘application/json’, it has to be blank (default to application/x-www-form-urlencoded I believe). Although it seems you have to SEND json, but without a name in the name value pair. Using JSON.stringify also messes this up. So the full working jQuery code is like this:

$.ajax({
    type: "POST",
    url: "api/slideid/reportexists",
    data: { "": reportpath },
    success: function(exists) {
        if (exists) {
            fileExists = true;
        } else {
            fileExists = false;
        }
    }
});

On the Web.API side, you MUST have the [FromBody] attibute on the parameter, but other than this it’s pretty standard. The real problem (for me) was the post.

In Fiddler, the request body looked like this “=%5C%5Croot%5Cdata%5Creport.html”

This post really had the answer, and linked to this article which was also very helpful.

Leave a Comment