AJAX Page - Executing functions after asynchronous callback

If you choose to make a callback with the data asynchronously, when the callback is done,
a client-side function is called.

With this function you can process the data returned by the server.
The function name is sent as the third parameter of the post function
(if this parameter is missing, the callback is done synchronously).

The syntax is:

        ob_post.post(null, "myServerMethod", myFunction);
	

In our next example we will send at the server the number 4556, on the server it adds 1000 to it and returns the response to the client.

If you want to execute a function depending on the response from the server
(such as an alert of the result or including parts of the result)
the function must have a parameter that will receive as value the response from the server.
This value can be of any type, thus you can manipulate it as you wish.

Here is an example of a function that alerts the data received from the server:

        function myFunction(result, ex)
        {
            // if there was no error
            if (ex == null)
            {
                // process response here
                alert("Data received from the server: " + result);
            }
        }
	
Click to test:




Note: Variable ex holds any error that might have occured.
Please visit the Test and Debug tutorial for details.

 
  • If you don't want to execute anything at the client when the callback is done
  • When you don't care if the callback is done or not
  • If the server will call a client method after it finishes execution (using ExecBeforeLoad or ExecOnLoad)
  • When you don't expect an answer from the server
but still use the asynchronous method, you can use an empty function as 3rd parameter.

Be aware that if you are using this method, you won't know if the callback is done succesfully and when it is finished. Here is how you have to call the post function:

        ob_post.post(null, "myServerMethod", function(){});
	

        public void myServerMethod()
        {
            // example of executing a client script before the processing starts
            ExecBeforeLoad(alert("some value"));
			
            // example of executing a client function after the processing is over
            ExecOnLoad("myFunction");
        }
	
Click to test:




See also: ExecBeforeLoad and ExecOnLoad tutorials.