Q:

JSON result problems in ASP.NET Web API when returning value types?

I'm learning aspnet mvc 4 web api, and find it very easy to implement by simply returning the object in the apicontrollers.
However, when I try to return value types such as bool, int, string - it does not return in JSON format at all. (in Fiddler it showed 'true/false' result in raw and webview but no content in JSON at all.
Anyone can help me on this?
Thanks.
Some sample code for the TestApiController:
public bool IsAuthenticated(string username)
{
    return false;
}

Some sample code for the jQuery usage:
function isAuthenticated(string username){
        $.getJSON(OEliteAPIs.ApiUrl + "/api/membership/isauthenticated?username="+username,
            function (data) {
                alert(data);
                if (data)
                    return true;
                else
                    return false;
            });
}

NOTE:  the jquery above returns nothing because EMPTY content was returned - however if you check it in fiddler you can actually see "false" being returned in the webview.
cheers.

A:

Before your callback function is called, the return data is passed to the jquery parseJSON method, which expects the data to be in the JSON format. jQuery will ignore the response data and return null if the response is not formatted correctly. You have two options, wrap you return boolean in a class or anonymous type so that web api will return a JSON object:
return new { isAuthentication = result }

or don't use getJSON from jQuery since you're not returning a properly formatted JSON response. Maybe just use $.get instead.
Below is a quote for the jQuery documentation:

Important: As of jQuery 1.4, if the JSON file contains a syntax error,
  the request will usually fail silently. Avoid frequent hand-editing of
  JSON data for this reason. JSON is a data-interchange format with
  syntax rules that are stricter than those of JavaScript's object
  literal notation. For example, all strings represented in JSON,
  whether they are properties or values, must be enclosed in
  double-quotes. For details on the JSON format, see http://json.org/.

