Hi I have a WCF Rest 4.0 project. For some reason I have a webservice which should return Json and it does if i hit the endpoint over fiddler but thru firefox or chrome if I type in the address I get xml. Whats going on???
Thanks for any help! Here's the code.
Web service in question:
[OperationContract]
[WebGet(UriTemplate = "",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public SomeObject [] GetObjects()
{
.....
Object code:
[DataContract]
public class SomeObject
{
[DataMember]
public string Date { get; private set; }
....
public String Site { get; private set; }
I posted this as a comment, but I'll add more detail here.
Your browser is most likely sending this header:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Note that it does not mention json but does mention xml.
Your WCF client is most likely using a different "Accept" header that gives a preference to json. You can check this in Fiddler.
If you are using the .NET 4.0 framework, this is the solution: http://karnicki.eu/2011/02/rest-wcf-net-4-0-service-with-json-jsonp-for-jquery/
WCF now has JSONP support out of the box with little configuration required.
Basically you just need to edit/add two config file entries, authenticationMode and StandardEndpoint, and voila, you can view json response from your WCF service in any browser.
EDIT: The original link is down - this might help: http://blog.shutupandcode.net/?p=696
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