Thought I would post follow up because I was also having a lot of trouble with Ajax.
So here is how it works.
GET requests have the data encoded within the URL, e.g yourserver.com?results=x. You access these on the receiving server using $_GET
POST requests have the data within the HTTP payload and not in the URL. You access these on the receiving server using $_POST
The URL field within Construct is the $_GET field
The DATA field within Construct is the $_POST field
POST usually allows a lot more data than GET, and you should use POST, unless you are only sending small amounts of data.
Can you use both at the same time? Sure, just put parameters within the URL field (GET) and also the Data field (POST). P.S Don't do this..just confusing :)
Choose GET (URL) for small stuff e.g results.php?token=1234&gender=M
Choose POST (DATA) for big stuff like sending arrays and dictionary's.
GET example
or with variables
In this example, we have set AJAX with our data contained within the URL. In the receiving script PHP etc. you would need to access using $_GET parameters.
HINT: The DATA field is blank "" meaning that we have made a empty POST request, and decided to include all the data within the URL, making it an empty POST request with GET parameters in the URL. This is probably the most confusing part, so choose either POST or GET and try to avoid using both.
<?php
print_r($_GET);
//Or:
foreach ($_GET as $key => $value)
echo $key . '=' . $value . '<br />';
?>
POST example
Or with variables
In this example, we have set AJAX with our data in the correct POST payload. In the receiving script PHP etc. you would need to access using $_POST parameters.
HINT: The DATA field now contains POST data and can be accessed using below.
<?php
print_r($_POST);
//Or:
foreach ($_POST as $key => $value)
echo $key.'='.$value.'<br />';
?>
Checking if you have success on the server
So now that PHP has received the request, if you want to communicate back to Construct on status, use the following code and save it as results.php on your server.
This code will create a Construct 2/3 compatible dictionary, where you can read it with Ajax completed function within Construct.
You may need to run it through phpformatter.com to make it pretty.
This example uses the GET example above.
<?php
//Setup token to look for
$token_required = "1234";
//See if we have $_GET request with field 'token'
if (isset($_GET['token']) && !empty($_GET['token'])) {
//Set $token_submitted variable to what was submitted
$token_submitted = $_GET['token'];
//Check the token to see if it matches
if (strcmp($token_submitted, $token_required) == 0) {
$data['c2dictionary'] = true;
$data['data']['status'] = 1;
$data['data']['message'] = "Token match! Open the pod bay doors HAL..";
//Display JSON results
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
} else {
$data['c2dictionary'] = true;
$data['data']['status'] = 0;
$data['data']['message'] = "Token does not match! I'm sorry Dave, I cannot do that..";
//Display JSON results
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
}
} else {
$data['c2dictionary'] = true;
$data['data']['status'] = 0;
$data['data']['message'] = "No token! Need to send token via GET request e.g yourserver.com/results.php?token=1234";
//Display JSON results
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
}
?>
Then open a browser for testing and go to:
Good token
https://yourserver.com/results.php?token=1234
Bad token
https://yourserver.com/results.php?token=1235
No token
https://yourserver.com/results.php?thisaintnotoken=1234
Happy with results in browser? Create new Ajax.completed in Construct and let it deal with the results like below.
have fun! Hope that helps