AJAX Question

0 favourites
  • 13 posts
  • Hello everyone,

    I'm starting to play with AJAX, so i added a text to behave as a button, then on the "On Click" i fire a AJAX action "POST TO URL" to a PHP file in my web server, the PHP file simply adds an entry in a database table.

    After clicking the button i go to see the table and i see a new registry is created but the field is empty.

    Set request header

    Header: "Access-Control-Allow-Origin"

    VALUE: "*"

    The post to url parameters are:

    TAG: "UUID"

    URL: "http://www.myserver.com/test/prueba.php"

    DATA: "lala"

    Method: "POST"

    if i execute the php file from the browser it adds the value just fine, for example:


    Could anyone advise?

    Thank you.

  • The PHP file requires the line with the access control.

    The tag value is used in c2 to identify the request/post, for like when its done.

    The data entry should have the key/values in it.


    data: "key=value&otherkey=othervalue"

    Also, in your console log, when testing the game, you can see the details of the post action (Ctrl + Shift + J , then select the network tab)

  • Hello Lennaert,

    I modified the data field.

    What do you mean the access control in the PHP file? I thought that was added in construct.

    What i did was to add a "Set request header" with:

    Header: "Access-Control-Allow-Origin"

    Value: "*"

    How would that go in a PHP file?

    Edit: Now i dont see a message error anymore in the console, but still doesnt update the value.

    Edit: Even if i export and upload the "game" to my webserver i still get the same results

    Thank you!

  • In your PHP file, below <?php, add


    header('Access-Control-Allow-Origin: *'); 
    ..rest of your code ...
  • Hi,

    Yeah, i tried adding that also tried uploading it to my webserver and its still showing up empty.

    This is the PHP File: http://imgur.com/a/oeVfb

    This is from construct: http://imgur.com/a/pUGxL and http://imgur.com/a/tI0mZ

    This is the DB: http://imgur.com/a/avG5z

    Thank you!

  • In the console, you can see the details of the request, you can even see if you get anything in return.

    Also, try removing the question mark in the data field, I don't think its needed.

    In c2 your using the Ajax.LastData ?

  • Hello,

    Removing the question mark didnt seem to do anything <img src="{SMILIES_PATH}/icon_e_sad.gif" alt=":(" title="Sad">

    Im not using Ajax.LastData, im not sure what is it for... that said, im not trying to retrieve anything yet im just trying to click on a button in my "game" and create an entry in my database table in the webserver...

    This is what i see on the console http://imgur.com/a/cw5RT

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In the console, check the network tab, and look for the adressss you use in the ajax request.

    Also, you can use an event to detect of your ajax request has been done, and or an event to catch an error.

  • Well, everything seems ok to me in the network tab, so i have no idea whats going on <img src="{SMILIES_PATH}/icon_neutral.gif" alt=":|" title="Neutral">

    Network Tab: http://imgur.com/a/o2SSV

  • Well, im new at c2... but this is starting to look more like a bug?

    I found out that if i do this it works:


    However if i fill the data field with anything and adjust the URL field accordingly it just doesnt work...

  • No, it's not a bug, you are trying to POST but you are then referencing $_GET[] array, the reason it's working there is that you are effectively formulating a GET request, try switching _GET[] to _POST[]

  • In your php script, how do you refer to the values?

    You should be using $_POST

    If that other method works, your perhaps using $_GET

    Edit: whoops missed that

    No, it's not a bug, you are trying to POST but you are then referencing $_GET[] array, the reason it's working there is that you are effectively formulating a GET request, try switching _GET[] to _POST[]

  • Ahhh, so dumb!

    Yeah it was that.

    Thank you both!

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