Service Stack Demo

Calling ServiceStack Web Services with Ajax

For performance reasons, the preferred to call ServiceStack services for Ajax clients is using JSON via the JSON endpoint at:
~/servicestack/json/reply/{ServiceName}

As this endpoint is just a standard JSON service we can use any ajax client to call them with. In this example we're using a light-wrapper around jQuery's $.ajax client.

1. First step is to initialize the ServiceClient

//Create a ClientGateway passing in the baseUrl where your services are hosted. var gateway = new servicestack.ClientGateway( location.protocol + "//" + location.host + '/ServiceStack.Examples.Host.Web/ServiceStack/');

2. After that calling a service becomes as easy as:

gateway.getFromService({ GetFactorial: { ForNumber: $("#txtGetFactorial").val() } }, function(e) { $("#serviceGetFactorial .result").html(e.Result); });

3. Try the live examples yourself:

Get Factorial Service

Result

Get Fibonacci Numbers Service

Result

Store New User Service

Result

Get Users Service

Result

4. Check out the other ways to call your services

One of the nice things about ServiceStack is that by using POCO classes to define your Service Interface we're able to seperate the 'message' (payload) from the communication 'channel' that delivers it.

This means that calling your services using XML is as easy as sending your request to a different end point. So calling your service via XML is as easy as:

  1. servicestack/xml/reply/GetFactorial?ForNumber=3
  2. servicestack/xml/reply/GetFibonacciNumbers?Skip=3&Take=20

Out of the box ServiceStack provides REST+JSON, REST+XML and POX, SOAP 1.1 and SOAP 1.2 end points.

The dynamic metadata summary page has a complete list of web services and end points that are available.

5. View the source code online or Download:

The complete source code for this example is viewable online or available to download as zip package from the link below: Download ServiceStack.Examples.zip