Skip to content

Z.ajax(...)

Studio is being deprecated, please head over to the documentation page for Mattercraft, our most advanced 3D tool for the web, where you can find the most recent information and tutorials.

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.

ParameterTypeDescription
paramsZ.Ajax.ParametersA set of parameters to configure the request.
callbackoptional, Z.Ajax.CallbackAn optional function to be called when the request finishes.

Z.Ajax.Request

// 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");
}
});