I have some WCF service code that is virtually the same, except one method is supposed to return a xml result, and the other a json result:
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
}
The xml works fine (e.g., when I enter “http://localhost:4841/RestServiceImpl.svc/xml/2468” into the browser).
However, when I enter “http://localhost:4841/RestServiceImpl.svc/json/2468” I get a “File Download – Security Warning” dialog, which allows me to save a file (named “2468” in this case), which contains the following when opened in Notepad:
{“JSONDataResult”:”You requested product 2468″}
Is this “as-designed” (to save json results to a file), or why doesn’t it act the same way as the xml-o-rama?
It’s up to your browser to determine how to handle any particular Content-Type, and it sounds like your browser doesn’t know what to do with JSON.
To check this, while using the Chrome Dev Console or Firebug (or whatever equivalent in whatever browser you use), look at the network requests when you load that resource. In the headers, you should see something like
If you do, the problem is your browser. If you don’t see that, your server or service code is wrong (and especially if it’s
application/octet-streamwhich is the mime-type equivalent of “I have no idea”).As far as browser, personally I recommend Chrome + this excellent plugin for displaying formatted JSON output: https://github.com/callumlocke/json-formatter