Z.ajax(...)

Z.ajax(params: Z.Ajax.Parameters, callback?: Z.Ajax.Callback) : Z.Ajax.Request;

Performs an AJAX request.

The server that responds to your request must include a “Access-Control-Allow-Origin: *” header in its response.


We highly recommend using HTTPS for all calls using this function. In addition to the security benefits for your users, Apple are deprecating HTTP-only network connections from apps.

Parameters

Parameter Type Description
params Z.Ajax.Parameters A set of parameters to configure the request.
callback optional, Z.Ajax.Callback An optional function to be called when the request finishes.

Returns

Z.Ajax.Request

Example

// Parameters to configure the AJAX request
const ajaxParameters : Z.Ajax.Parameters = {
    url: "https://s3-eu-west-1.amazonaws.com/wizcontent/test/zappar.json",
    method: "GET",
    timeout: 3000
};

// Perform the AJAX request
Z.ajax(ajaxParameters, (statusCode, data, request) => {

    if (statusCode === 0) {
        locationText.text("Unable to connect - check network connection.");
        return;
    }

    if (statusCode < 200 || statusCode >= 300) {
        locationText.text("HTTP request failed: " + statusCode);
        return;
    }

    // Attempt to parse the data returned from the AJAX request as JSON
    let parsedData;
    try {
        // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
        parsedData = JSON.parse(data);
    } catch (e) {
        locationText.text("Unable to parse JSON: " + e);
        return;
    }

    // Check the data is as expected and display it
    if (parsedData.company) {
        locationText.text(parsedData.headOffice);
    } else {
        locationText.text("Unexpected response from API");
    }

});
zapcode branded_zapcode i