jquery wcf数据服务返回atom+xml而不是json

标签 jquery wcf json

本地一切正常,但是当我将 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>

fiddler

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

最佳答案

我认为巴勃罗·卡斯特罗的answer这个问题就是您正在寻找的解决方案。

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

编辑:如果您使用 Fiddler 检查您的请求,并且没有看到“Accept” header 被设置或未正确设置,则您可以在此使用此技术文章:

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

希望这有帮助!

关于jquery wcf数据服务返回atom+xml而不是json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5436333/

相关文章:

jquery - 如何定位复选框和文本

wcf - 我可以在 WCF 服务中将 wsHttpBinding 与 webHttpBinding 一起使用吗?

c# - Wcf 跟踪错误到 Windows 服务

angularjs - HTTP POST 变成 OPTIONS AngularJS

javascript - jQuery .hide() 我做错了什么

jquery - 显示/隐藏 div 不起作用

javascript - $.getScript() : Loading js scripts conditionally

wcf - 首次 WCF 服务调用时出错

java - 是否可以使 JsonFormat 日期时间的某些部分可选?

ios - 使用 YouTube API 获取 YouTube 视频的标题