2010-09-10 95 views
3

我想从jquery调用wcf自我托管的方法,但是,我总是得到消息:“方法不允许”。我发现这个论坛上的这个问题的一些答案,但没有对我工作.... PS。当我在控制台应用程序中添加引用并使用它时,它工作正常。wcf自我托管jquery

它是一个窗口形成自托管应用程序

形式负载:

ServiceHost host = new ServiceHost(typeof(MyServices), new Uri[] { }); 
host.Open(); 

App.Config中

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

经营合同:

[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); 
    } 
}); 

回答

2

那么,当前的问题是由以下事实WebInvoke,当你不指定Method参数,将默认为 “POST” 造成的。你的jQuery代码发出一个“GET”请求,所以“方法不允许”错误将被期望。

但是,您最初尝试GET并得不到任何回报的评论使我倾向于使用浏览器违反same origin policy。由于您在Windows窗体中使用地址http://localhost:8766托管服务,因此我假设您的网站不在'http://localhost:8766'处。每同源策略:

术语“原产地”使用 域名的定义,应用层 协议,并(在大多数浏览器)TCP 运行 脚本的HTML文档的端口。两个资源被认为是 是相同的来源当且仅当 如果所有这些值完全相同 相同。

如果您以JSON格式从服务中返回数据,则可能需要考虑使用返回JSONP的GET请求。这种类型的请求不会违反相同的源策略,因为允许请求远程脚本。然后,您的通话jQuery的是这样的:

$.ajax({ 
    url: "http://localhost:8766/Test", 
    dataType: "jsonp", 
    processData: false, 
    type: "GET", 
    success: function (msg) { 
     console.log(msg); 
    } 
}); 

由于您使用WebInvoke,您可能还需要使用WebHttpBinding结合,而不是BasicHttpBindingWebInvoke不起作用,除非它与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,引导您完成这一点。

希望这有助于!