Namespace: server

mx. server

Handle server communication.

Methods

(static) get(args)

Fetches a resource from the server using the HTTP GET method.

Parameters:
Name Type Description
args
Properties
Name Type Description
url string

url from which to fetch the resource

handleAs "text" | "json" | "xml"

format in which to deliver the response

load mx.server~XhrSuccessCallback

function to handle the response

error mx.server~XhrErrorCallback

function to handle errors

Example
mx.server.get({
    url: "/data/files/dogs.txt",
    handleAs: "text",
    load: function(response) {
        console.log("dogs", response);
    },
    error: function(e) {
        console.log("failed to retrieve resource");
    }
});

(static) getCacheBust() → {string}

Gets the cache bust used to refetch assets after a redeployment.

Returns:

cache bust to append to an URL

Type
string
Example
mx.server.getCacheBust(); // Get the cache bust e.g. "635161595264900996"

(static) request(args)

Fires a request to the server.

To fetch objects or invoke microflows, mx.data.get or mx.data.action should be used instead.

The args.options.callback argument is only called when the request was successful. Its arguments are the state of the request and the response from the server in JSON format.

When the request fails and args.options.error was passed, it receives a standard Javascript Error object. When args.options.error was not passed, the mx.onError function is called on which you can subscribe using dojo/aspect::after. Typically, the UI does this to show an error message for unhandled errors.

Parameters:
Name Type Description
args
Properties
Name Type Description
request.action string

action to trigger on the server

request.params Object

parameters to pass to the action

request.context mendix/lib/MxContext

context to pass to the action

options.unique boolean

whether this request may be merged with an already sent request with the exact same parameters

options.preventCache boolean

prevent the response coming from the browser cache

options.store.caller Object

caller instance on which server instructions will be executed

options.sync boolean

whether the request should be synchronous or asynchronous

options.standalone boolean

whether changes may be sent to the server before executing this action

options.callback mx.server~RequestSuccessCallback

function to handle the result when successful

options.onValidation mx.server~RequestValidationCallback

function to handle validation feedback

options.error mx.server~RequestErrorCallback

function to handle errors

Example
define([
  "mendix/lib/ConnectionError",
  "mendix/lib/ValidationError",
  "mendix/lib/Error"
], function(ConnectionError, ValidationError, MxError) {

mx.server.request({
    request: {
        action: "rollback",
        params: {
            guid: "2251799813686950"
        }
    },
    options: {
        callback: function(state, response) {
            console.log("rollback successful");
        },
        error: function(e) {
            if (e instanceof ConnectionError) {
                console.log("connection error occured: " + e.message);
            } else if (e instanceof ValidationError) {
                console.log("validation error occured: " + e.message);
            } else if (e instanceof MxError) {
                console.log("some other Mendix error occured: " + e.message);
            } else {
                console.log("some generic error occured: " + e.message);
            }
        }
    }
});

});

Type Definitions

RequestErrorCallback(error)

Error callback for requests.

Parameters:
Name Type Description
error Error

error describing the failure of the request

RequestSuccessCallback(dummy, response)

Success callback for requests.

Parameters:
Name Type Description
dummy string

Dummy parameter that exists for historical reasons. Always set to 'load'.

response Object

response of the request

RequestValidationCallback(validations)

Validation callback for requests.

Parameters:
Name Type Description
validations Array.<mendix/lib/ObjectValidation>

validations received for the request

XhrErrorCallback(error)

Error callback for XHRs.

Parameters:
Name Type Description
error Error

error describing the failure of the request

XhrSuccessCallback(response)

Success callback for XHRs.

Parameters:
Name Type Description
response string | object | Document

response for the XHR, depending on the args.handleAs parameter of the original request