2011-10-30 109 views
0

我似乎無法即使我用標籤的屬性與方法得到了JSON WCF服務的成功的:無法獲取JSON了WCF服務

[WebGet(UriTemplate = "Product/{productIdString}", 
ResponseFormat = WebMessageFormat.Json)] 

OR

[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 

我總是收到XML,不管我是將它作爲DataSet還是List<>返回。

工作的唯一方法是手動將JSON作爲字符串返回,但它也封裝在XML中。

任何線索?

+0

您在服務中使用了哪些**綁定**? JSON的東西幾乎只在'webHttpBinding'(REST風格的WCF服務)上工作 –

回答

2

嘗試這樣的事:

[WebInvoke(Method = "GET", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "products")] 
    public IList<JxProduct> GetProductList() 
    { 
     List<JxProduct> products = new List<JxProduct>(); 
     products.Add(new JxProduct { Description = "Tire", Id = 1, Price = 39.99}); 
     products.Add(new JxProduct { Description = "Tube", Id = 2, Price = 4.99 }); 
     products.Add(new JxProduct { Description = "Patch", Id = 3, Price = 3.99}); 

     return products; 
    } 

您還可以查看下面的帖子裏面介紹了有關web.config設置的更多細節。 How do I return clean JSON from a WCF Service?

+0

這是非常有用的...巨大的感謝:) –

0

[WebGet](和[WebInvoke])屬性只能被WebHttpBehavior<webHttp/>如果您使用配置)識別。確保您正在點擊的端點具有該行爲設置。