What is the DATA field in the AJAX Post?

0 favourites
  • 3 posts
From the Asset Store
A very simple to set up ZElevation based depth of field system for your game. Fully commented.
  • What is the DATA field used for?

    It isn't clear to me after reading the Construct Manual on it and most examples I see are from old versions and don't have that field.

    Is it to move the "?category1=data1&category2=data2" from the URL field to the DATA field? If so, is there any advantage to doing so? Or simply for organization?

    Bonus question: After a AJAX post is a return text/value stored in AJAX.LastData? Such as: echo 'success'. Separate question and testing it out now but thought anyone answering the first might know...two birds/ajax with one stone/post.

    Thanks

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I still haven't figured out the answer to the two above questions but have another issue that thought may be related:

    My AJAX post request is successfully submitting data to the mySQL table (I can pull up the table and see the entry added) but the events for the On tag error are firing instead of the On tag completed.

    How does Construct determine whether the POST was successful?

    I thought it was just doing this when previewing but I exported and uploaded to same server (as the table) and still having the error events firing.

    Thought this could have to do something with the Data field I'm leaving empty...

    I am still searching the forums and tutorials for a solution but most examples aren't detailed enough or are old and don't have the DATA field.

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)