The
AJAX plugin allows web pages to be requested during the running of the game. Its name derives from "Asynchronous JavaScript and XML", a technique familiar to most web developers. The AJAX object is designed for use by web developers already familiar with the technique - this reference will not describe the principles of AJAX itself, only how to use it in Construct 2 with the AJAX object. Generally using the AJAX object also requires custom server-side scripting which is beyond the scope of this manual.
How to make a request
The basic usage of the AJAX object consists of:
1. Use the
Request action to load a URL.
2. A moment later after the request completes,
On completed triggers.
3. The
LastData expression can be used to access the content of the response.
The
tokenat system expression may be useful to split simple responses. Alternatively, you could load
LastData in to the
XML plugin to read it as an XML document.
Tags
A different tag can be provided for each request. This is a simple string you set to tell apart different requests. For example, on startup you may request both
foo.php with tag
"foo" and
bar.php with tag
"bar". When the first request completes,
On "foo" completed triggers; when the second request completes,
On "bar" completed triggers. Requests can complete in a different order to the order they were made, so without tags it would be impossible to tell which request was completing.
Making AJAX requests cross-domain or in preview
By default, browsers
block AJAX requests across domains. This means, for example, a game on
scirra.com can request other pages on
scirra.com, but cannot request pages on
facebook.com. This is an important security feature of web browsers (it is not specific to Construct 2 or its AJAX object).
Also, when previewing in Construct 2 the game runs on
localhost. This counts as a different domain to the rest of the internet, so typically AJAX requests to
any web page will fail during preview,
unless the server explicitly allows cross-domain requests.
If you want AJAX requests to your server to work from any domain, or in preview, you can configure it to send the following HTTP header:
Access-Control-Allow-Origin: *This will enable AJAX requests from any domain, but you should still be aware of the possible security implications of this. For more information on cross-domain requests see
HTTP access control on MDN.
AJAX conditions
On completedTriggered when a request with the same tag has completed successfully. The
LastData expression contains the response.
On errorTriggered when a request with the same tag has failed. This can be for a number of reasons, such as the server being down or the request timing out. (The
LastData expression is not set since there is no response.)
AJAX actions
RequestRequest the contents of a URL. A tag is provided to match it up with the
On completed and
On error triggers.
AJAX expressions
LastDataThe contents of the last response. This is set in the
On completed trigger. If used in a different event, it contains the response of the last completed request. The
tokenat system expression may be useful to split simple responses