views:

208

answers:

3

I am not sure what is wrong with my JSON response from WCF but it is not being parsed properly. I probably made a simple mistake, hopefully someone can spot it. I am using VS 2008 Pro SP1.

When I navigate directly to the url Chrome thinks it is a file download. When I hit say flickr's service it returned a string inline in the browser body, which makes me think my content is being sent incorrectly.

Service

[ServiceContract]
public interface IFoo
{
    [OperationContract]
    [WebGet(UriTemplate = "/foos/", ResponseFormat = WebMessageFormat.Json]
    Foo[] GetFoos();
}

Model

// this is in a separate assembly from the service
[DataContract]
public class Foo
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}

JQuery

$(document).ready(function() {
    $.getJSON(fooUrl, function(data) {
        alert(data); // data is an object, but data.items is null
            // this complains 'length is null or not an object'
            $.each(data.items, function(i, item) { 
                fooList.append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
            });
        });
    });

When I look at the response in Fiddler I see:

 [{"Id":1,"Name":"Foo1"},{"Id":2,"Name":"Foo2"},{"Id":3,"Name":"Foo3"}]

Update

Here are the raw headers from fiddler for the response comparison (me vs. flickr)

Mine

HTTP/1.1 200 OK
Content-Length: 162
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0

Flickr

HTTP/1.1 200 OK
Date: Sat, 12 Dec 2009 00:30:36 GMT
P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Sat, 12 Dec 2009 00:26:19 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding
Connection: close
Transfer-Encoding: chunked
Content-Type: application/x-javascript; charset=utf-8

It looks like the big difference is I have application/json and they have application/x-javascript. As I noted above I am using WebMessageFormat.Json on the operation contract.

Thanks for any help.

A: 
$.each(data, function(i, item)

remove the .items

F.Aquino
Thanks for the reply, I will try it out later. Interestingly, I copied my JQuery from http://docs.jquery.com/Ajax/jQuery.getJSON
blu
I am using 1.3.1 on my project (I have the exact same routine running)
F.Aquino
This results in a list of "undefined"
blu
Wouldnt the case in the items matter now? data[i].Id not id etc
F.Aquino
+1  A: 

I believe that application/json is the correct media type for what you are doing.

The fact that the browser does not render it and simply gives you a file download dialog is not unsurprising. Browsers only render directly a very small set of media types.

It does not mean that there is anything wrong with what your web service is returning. It just means that the browser does not have any built in capability to convert json into an html representation for display purposes.

If you retrieve this json representation in your client code you should be able to handle as you do any other json data.

Darrel Miller
Ok thanks for this, it narrows down what is wrong.
blu
A: 

I'm having the same issue, calling $.getJSON(url, function(data)) returns data as null. I am using WCF,.NET 3.5. The WCF itself returns data correctly with application/json but getJSON does not seem to understand it. The WCF is returning an array of json objects. Has this been resolved? Thanks!

vasya