开发者

RESTful Service Method in WCF

开发者 https://www.devze.com 2022-12-13 15:30 出处:网络
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.

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
开发者_如何学CLast-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.


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.


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

remove the .items

0

精彩评论

暂无评论...
验证码 换一张
取 消