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.
Parameters
Section titled “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
Section titled “Returns”Example
Section titled “Example”// Parameters to configure the AJAX requestconst ajaxParameters : Z.Ajax.Parameters = { url: "https://s3-eu-west-1.amazonaws.com/wizcontent/test/zappar.json", method: "GET", timeout: 3000};
// Perform the AJAX requestZ.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"); }
});