2012-03-13 74 views
2

我在网上看到一堆示例,但我很困惑这个服务应该是独立的还是与消费应用程序在同一个解决方案中。有人可以帮帮我吗?如何使用jQuery将AJAX调用添加到ASP.NET网页的WCF Web服务?

  1. 我需要支持AJAX型WCF服务的文件添加到我的ASP.NET Web应用程序或我需要一个Web服务引用到我的WCF服务?

  2. 当我使用jQuery的Ajax的功能有一个URL参数。我看到的所有例子都使用“Services/MyService.svc/MyMethodName”。这是因为服务在Web应用程序的解决方案中?

  3. 请问我的WCF服务必须有:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

  4. 做我的web服务调用需要具有以下属性?

    [WebInvoke(方法= “POST”, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]

  5. 难道我的Web应用程序或我的web服务需要的行为设置为enableWebScript

我是否错过任何必需的属性或设置?

这里是我的web应用程序的jQuery我的Ajax请求:

$.ajax({ 
type: "POST", 
url: "http://myserver/myservice.svc/mymethod", 
contentType: "application/json; charset=utf-8", 
data: "{" + args + "}", 
dataType: 'json', 
}); 

这里是我的web服务的web.config:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="Basic" /> 
     </basicHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="ServiceBehavior" name="MyServiceName"> 
     <endpoint address="basic" 
        binding="basicHttpBinding" 
        bindingConfiguration="Basic" 
        name="Basic" 
        contract="IService" /> 
     <endpoint address="web" 
        behaviorConfiguration="webHttpBehavior" 
        binding="webHttpBinding" 
        name="Web" 
        contract="IService" /> 
     <endpoint address="mex" 
        binding="mexHttpBinding" 
        name="Metadata" 
        contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="webHttpBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

回答

3
  1. 没有,你可以直接调用它
  2. 有点。这是一个相对路径,基于网页与服务在您的托管环境中的位置
  3. 如果您计划POST到服务方法,那么是。
  4. 是,在服务

在您的服务端点的行为包括

<serviceMetadata httpGetEnabled="true"/> 

+0

我不明白#2。如果我的asp.net Web应用程序在解决方案中没有Web服务,但Web服务托管在开发服务器上,那么我的网址应该是什么?我是否需要对开发服务器的http使用绝对地址? – hyprsleepy 2012-03-14 20:36:26

+0

绝对路径总是有效的,WCF不关心你使用哪一个,只要他们解决。就你而言,这听起来像你需要一个绝对的服务网址。 – PTiddy 2012-03-14 20:56:34

+0

如果我使用JSON,我的WCF服务需要比basicHttpBinding多吗?我需要webHttp吗? – hyprsleepy 2012-03-16 14:24:18