Invokes a particular method using the API Server/Worker combination specified in a previous call to utilCreateAPIServer.
When working server-side .utilServerAPIServerCall can be used to communicate with the API Server - it's a lot simpler to use.
This function will invoke one of two callback functions, returning the results of the call or any error information.
It can be called client or server-side. It does behave differently.
Server-side calls are synchronous: the callback functions will be invoked before this function returns. Client-side calls are performed using AJAX and are asynchronous: the callback functions will, probably, be invoked at some point in the future after this call returns. As a general rule nothing should be performed after the call that depends on the result of the calls. Any subsequent actions should be performed in the callback functions.
Executed
Client or server-side. Note that API Server Security will restrict which workers are available client-side.
Arguments
Argument | Type | Description |
---|---|---|
name | String, required | The name of the API Server connection. This must have been created previously in a call to .utilCreateAPIServer |
method | String, required | The name of the method to invoke |
params | Object, required | The object will contain the named parameters passed to the worker method being invoked |
okCB | Function, required | The function to be called when the results of the calling method are available. It is called with a single parameter that is the result of the call |
errorCB | Function, optional | Function to be called when there is any kind of error calling the method. It is called with two parameters, any response data and an error message. |
Example
In this example the function has been placed in the handler of a text field. The worker is the publicly available ajaxlibrary worker, the method a simple End Point "formstraining.crypto" which generates a hash of the string passed to it.
When the value of the text field changes, the text is passed to the End Point and the result updates a guidance text field, "GUIDANCE".
function(helper, fieldName, value, valid, eventName) {
if (eventName == 'CHANGE' && valid) {
helper.utilCreateAPIServer('APIServer', '/apiserver/ajaxlibrary', {});
helper.utilInvokeAPIServer('APIServer', 'formstraining.crypto', {
'text': value
},
//The ok function. Update the guidance text with the successful result
function(data) {
helper.setFieldValue('GUIDANCE', 'The hash of the text you just entered: ' + JSON.stringify(data));
},
//The error function. Update the guidance text with any error message
function(errorDetails, errorMessage) {
helper.setFieldValue('GUIDANCE', 'apiServercall ERROR: ' + errorMessage + " " + JSON.stringify(errorDetails));
}
);
}
}