2011-03-25 98 views
2

本地一切工作正常,但是當我將wcf數據服務部署到服務器時,響應是atom + xml。我錯過了什麼嗎?jquery wcf數據服務返回原子+ xml而不是json

jQuery的

 $(function() { 
       $.ajax({      
        contentType: "application/json; charset=utf-8", 
        url: "http://subdomain.domain.com/wcfdataservice.svc/surveys/", 
        dataType: "jsonp", 
        success: function (data, textStatus) { 

        }, 
        error: function (data, textStatus) { 

        } 
       });  

     }); 

WCF的web.config

<system.web> 
    <compilation debug="false" targetFramework="4.0" /> 

<authentication mode="None"/> 

</system.web> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
</system.webServer> 
<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    <standardEndpoints> 
     <webHttpEndpoint> 
      <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true" /> 
     </webHttpEndpoint> 
    </standardEndpoints> 

</system.serviceModel> 

提琴手

 HTTP/1.1 200 OK 
Cache-Control: no-cache 
Content-Length: 13483 
Content-Type: application/atom+xml;charset=utf-8 
Server: Microsoft-IIS/7.5 
DataServiceVersion: 2.0; 
X-AspNet-Version: 4.0.30319 
X-Powered-By: ASP.NET 
Date: Fri, 25 Mar 2011 17:46:37 GMT 

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<feed xml:base="http://subdomain.domain.com/wcfdataservice.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> 
... 

回答

2

我覺得巴勃羅·卡斯特羅answer這個問題是你要尋找的解決方案。

基本上,您需要在請求中指定「application/json」的「接受」標頭,還可以選擇使用服務上的[JSONPSupportBehavior]屬性。我想,既然你控制了兩端,那麼接受頭就是你所需要的。

編輯:如果您檢查,與提琴手,你的要求,你沒有看到「接受」被設置與否頭正確設置,您可以在這篇文章中使用這種技術:

http://snipplr.com/view/9869/set-jquery-ajax-header/

希望這有助於!

0

現在使用WCF WebApi(預覽6)更容易做到這一點。你必須使用NuGet來安裝WCF WebApi,但這是值得的。安裝後,請查看here瞭解更多信息。