【发布时间】:2012-07-25 22:39:45
【问题描述】:
我已经构建了一个 WCF Web 应用程序,将它的方法暴露在 get enabled 方法中
[OperationContract]
[WebGet]
string getStatistics();
[OperationContract]
[WebGet]
string getVenues(string BrandName, int limit);
并编辑了配置文件:
<endpoint address="json" binding="webHttpBinding" contract="foursquare2RDF.IVenue2rdf" behaviorConfiguration="restBehavior"/>
在服务行为中:
<endpointBehaviors>
<behavior name="restBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
我在 IIS 上托管该服务,它在浏览器上运行良好,所以当你点击时:
http://localhost:83/venue2rdf.svc/json/getStatistics
效果不错
问题是如果显示这些错误,我就无法使用这个宁静的服务:
OPTIONS http://localhost:83/venue2rdf.svc/json/getStatistics?{'venues':'100'} 405 (Method Not Allowed)
XMLHttpRequest cannot load [http://localhost:83/venue2rdf.svc/json/getStatistics][1]. Origin null is not allowed by Access-Control-Allow-Origin.
我正在使用该代码来调用服务:
$.ajax({
type: "get",
url: statisticsURL,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
eval("var x = " + msg.d);
console.log(x);
}
});
到目前为止我达到了什么:
- 我尝试将 $.ajax 替换为 $.getjson,如类似问题中所述 并且错误 405 被删除,第二个错误刚刚出现
- 我找到了一个名为 Ajax 的 WCF 服务项目,但我仍然不想迁移到新项目中
- 我知道有类似的问题,但都不适合,显示我的不同错误
【问题讨论】:
-
如果您调用的项目与您的 WCF 服务在同一个域中,那么来自 JQuery 的调用是可能的。如果域不同,那么您需要使用 @the_ajp 所说的 JSONP,并且您需要通过将结果写回响应流来确保您的 WCF 处理 JSONP 请求。
标签: wcf jquery get http-status-code-405