我正在尝试从 jquery 调用 wcf 自托管方法,但是我却收到消息:“不允许使用该方法”。我在这个论坛上找到了这个问题的一些答案,但对我来说没有任何作用...... 附:当我在控制台应用程序上添加引用并使用它时,它工作得很好。
这是一个 Windows 窗体自托管应用程序
表单加载:
ServiceHost host = new ServiceHost(typeof(MyServices), new Uri[] { });
host.Open();
应用程序配置
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceConfig" name="MyServices">
<endpoint address="srv" binding="basicHttpBinding" contract="Operations" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8766" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceConfig">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
运营契约(Contract):
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Test();
服务:
public string Test()
{
return "Good!";
}
JQuery
$.ajax({
url: "http://localhost:8766/Test",
contentType: "application/json",
processData: false,
//data: '{}', // tried passing data
type: "GET", // tried POST and pass nothing(deleting this param), but nothing...
success: function (msg) {
console.log(msg);
}
});
最佳答案
嗯,您当前的问题是由于 WebInvoke
当您未指定 Method
参数时,将默认为“POST”造成的。您的 jQuery 代码正在发出“GET”请求,因此会出现“不允许的方法”错误。
但是,您关于最初尝试 GET 但没有得到任何返回的评论使我倾向于您的问题违反了 same origin policy使用您的浏览器。由于您在 Windows 窗体中托管该服务,地址为 http://localhost:8766
,因此我假设您的网站不位于 ` http://localhost:8766 '。根据同源策略:
The term "origin" is defined using the domain name, application layer protocol, and (in most browsers) TCP port of the HTML document running the script. Two resources are considered to be of the same origin if and only if all these values are exactly the same.
如果您以 JSON 格式从服务返回数据,您可能需要考虑使用返回 JSONP 的 GET 请求。这种类型的请求不会违反同源策略,因为允许请求远程脚本。您的 jQuery 调用将如下所示:
$.ajax({
url: "http://localhost:8766/Test",
dataType: "jsonp",
processData: false,
type: "GET",
success: function (msg) {
console.log(msg);
}
});
由于您使用的是 WebInvoke
,因此您可能还需要使用 WebHttpBinding
绑定(bind)而不是 BasicHttpBinding
。 WebInvoke
除非与 WebHttpBinding
一起使用,否则不起作用。当您使用 WebHttpBinding
时,您可以对其进行自定义,以便它响应 JSONP 请求。所以你的配置将如下所示:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceConfig" name="MyServices">
<endpoint address="srv" binding="webHttpBinding" contract="Operations" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="webHttpBehavior" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8766" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceConfig">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
这是一个非常好的博客post这会引导您完成此操作。
希望这有帮助!
关于jquery - wcf自托管jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684641/