Calling Web Services from Client Script
First you must expose your service to be consumable from client script. For a standard XML Web service, add the ScriptServiceAttribute to your .asmx.cs file. If you work from template, you just need to uncomment the line. Your result would look like this:
To set up a WCF service, you should ensure of the following:
- The service must be hosted by a web application within a .svc file. You will need to point to this file later.
- You must configure the web application to support service calls from client script.
To configure the web application, you need to do the following:
- In the web.config, set the aspnetCompatibilityEnabled attribute of the serviceHostingEnvironment property to true.
- Set up the binding and service behaviors.
- Add an endpoint.
Your web.config should look like this when you’ve done:
To consume the service from client script, you should add a ScriptManager control to your client page. Then define a Services tag inside of it, and add ScriptReferences for the scripts you want to include. A ScriptReference can have two attributes, a Path and an InlineScript property. The Path should be set to the .asmx or .svc file (depending on the type of service you are using). The InlineScript is set to false by default, set it to true when the proxy code of your service is defined in the same page. An example:
ServiceName.MethodName(value, onSuccess, onFail);