hyprsleepy
hyprsleepy

Reputation: 1810

How do you add an AJAX call to a WCF web service to an ASP.NET web page using jQuery?

I see a bunch of examples online but I am confused as to whether the service should be stand-alone or in the same solution as the consuming application. Can someone please help me?

  1. Do I need to add a file of type AJAX-enabled WCF Service to my ASP.NET web application or do I need a Web Service reference to my WCF Service?

  2. When I use the jQuery Ajax function there is a URL parameter. All the examples I see use "Services/MyService.svc/MyMethodName". Is this because the service is inside of the web application's solution?

  3. Does my WCF Service have to have:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

  4. Do my web service calls need to have the following attributes?

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]

  5. Does my web application or my web service need the behavior set to enableWebScript?

Did I miss any required attributes or settings?

Here is my jquery ajax request in my web application:

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

Here is my web service 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>

Upvotes: 2

Views: 3217

Answers (1)

PTiddy
PTiddy

Reputation: 126

  1. no, you can call it directly
  2. sort of. That is a relative path based on where the web page vs service are in your hosting environment
  3. yes
  4. if you plan to POST to the service method, yes.
  5. yes, on the service

Include

   <serviceMetadata httpGetEnabled="true"/> 

in your service endpoint behavior.

Upvotes: 3

Related Questions