jquery wcf数据服务返回atom+xml而不是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">
...
Locally everything works fine but when I deploy the wcf data service to server the response is atom+xml. Am I missing something?
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">
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为巴勃罗·卡斯特罗对这个问题的答案是您正在寻找的解决方案。
基本上,您需要在请求中指定“application/json”的“accept”标头,还可以选择在服务上使用
[JSONPSupportBehavior]
属性。我认为,既然你控制两端,那么接受标头就是你所需要的。编辑:如果您使用 Fiddler 检查您的请求,并且没有看到“Accept”标头被设置或未正确设置,则您可以在本文中使用此技术:
< a href="http://snipplr.com/view/9869/set-jquery-ajax-header/" rel="nofollow noreferrer">http://snipplr.com/view/9869/set-jquery-ajax- header/
希望这有帮助!
I think Pablo Castro's answer to this question is the solution you're looking for.
Basically, you need to specify an "accept" header of "application/json" in your request, and also optionally use the
[JSONPSupportBehavior]
attribute on your service. I think, since you're controlling both ends, the accept header is all you need.EDIT: If you examine, with Fiddler, your request and you don't see the 'Accept' header being set or not being set correctly, you may be able to use this technique in this article:
http://snipplr.com/view/9869/set-jquery-ajax-header/
Hope this helps!
现在使用 WCF WebApi(预览版 6)可以更轻松地做到这一点。您必须使用 NuGet 安装 WCF WebApi,但这是值得的。安装完成后,请查看此处了解更多信息。
This is much easier to do now using WCF WebApi (preview 6). You'll have to install the WCF WebApi using NuGet, but it's worth it. Once you have it installed, look here for more information.