【问题标题】:jquery wcf data service returns atom+xml instead of jsonjquery wcf数据服务返回atom+xml而不是json
【发布时间】:2011-03-25 18:03:02
【问题描述】:

本地一切正常,但是当我将 wcf 数据服务部署到服务器时,响应是 atom+xml。我错过了什么吗?

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">
...

【问题讨论】:

    标签: jquery wcf json


    【解决方案1】:

    我认为 Pablo Castro 对这个问题的 answer 是您正在寻找的解决方案。

    基本上,您需要在请求中指定“application/json”的“accept”标头,并且还可以选择在您的服务上使用[JSONPSupportBehavior] 属性。我认为,由于您控制两端,因此您只需要接受标头。

    编辑:如果您使用 Fiddler 检查您的请求,但没有看到“接受”标头被设置或设置不正确,则您可以在此使用此技术文章:

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

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      现在使用 WCF WebApi(预览版 6)更容易做到这一点。您必须使用 NuGet 安装 WCF WebApi,但这是值得的。安装后,请查看here 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 2020-06-02
        • 1970-01-01
        • 2012-02-26
        • 2023-04-05
        • 2012-06-20
        相关资源
        最近更新 更多