Ajax JSON Method Callbacks

The following examples call methods on the server from the client, returning typed result values that are displayed on the client without PostBacks. The text beside the header shows the server side method signature called. To see what's happening I recommend you hook up an HTTP viewer like Fiddler to watch the HTTP traffic.

Mandatory Hello World

Demonstrates a very basic callback with a single string parameter


Enter your name:
Number Addition

This example demonstrates that you can pass simple types rather than just strings from the client to the server.


+

FoxPro Cursor Data

This example demonstrates an AJAX callback that returns a FoxPro cursor that is loaded into the list


FoxPro Objects

This example demonstrates returning an object from FoxPro and embedding the data into the page. To update select an item from the Customer List above which fetches the specified customer from the server.


Company:

Name:

Address:

 

Web Connection Process Class Callbacks

You can also use JSON callbacks to non-WebControl Framework Process classes and in fact this is a good idea for 'services' that provide all AJAX callbacks for a given application.


Retrieve a stock quote:


This sample demonstrates a variety of ways to return data from the server into the client page. The control supports Page Callbacks where the code in the browser can callback onto the same page on the server and return the result from a method call with fair support for typed result data via JSON encoding. This means you can get result values such as integers, date time values, even objects and cursors returned to you from the server. Cursors are marshalled to an object which contains an array of Rows and proeprties for each of the fields.

Remote method calls are initiated by hooking an event - all the above are fired off buttons and one onchange event on the listbox. These events then call the Proxy.callMethod() function in the West Wind Client Library where Proxy is the id of the AjaxCallbackMethod control on the page. A sample call looks like this:

Proxy.callMethod('Helloworld',[$("#txtName").val()],
			function(result) {
				alert("Returned: " + result);												 
			},onPageError);

The first parameter is the method, followed by an array of arguments that are marshalled to the server via JSON. Each value can be of any type and is encoded into the appropriate JSON format. The server side control turns these parameters into arguments to the method to be called. The final two parameters are a callback handler that receives the result of a callback and an error handler.

Currently JSON marshalling is only supported from the server to the client. Any parameters passed from the client to the server are passed as plain strings. The problem is that FoxPro doesn't provide runtime type information, so there's no easy way to marshal the parameter values to a proper type. There are a few options to make this work, but in this initial release only raw string input parameters are supported.

You also have access to POST data if you choose. The control lets you control how data is sent to the server. Method calls post parameters, but you can also optionally have it send up all the form variables from the current clientstate. What's really cool about this is that hte page framework automatically assigns these to the controls of the page so in many cases you can reference - this.txtName.Text just as you would with a full page postback! This may actually be the preferred way to return data back to the client.