Luke Belbina
Luke Belbina

Reputation: 5869

WCF ResponseFormat JSON Returns Json in Fiddler, Xml in Chrome/Firefox!

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; }

Upvotes: 6

Views: 4569

Answers (4)

fjxx
fjxx

Reputation: 945

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

Upvotes: 1

enguerran
enguerran

Reputation: 3291

I had the same problem. I used a WCF configuration with no svc.

I had to change this boolean in web.config from true :

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>

to false :

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>

Upvotes: 0

Foole
Foole

Reputation: 4850

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.

Upvotes: 2

Raghu
Raghu

Reputation: 2758

Browsers cant display raw JSON, however you can use a tool like JSON viewer (http://jsonviewer.codeplex.com/wikipage?title=Home&ProjectName=jsonview) or Fiddler is fit for that job too.

Upvotes: 0

Related Questions