How do I do a simple AJAX request and retrieve?

0 favourites
  • 5 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • I saw a post from a while ago where someone was able to do what I want, but I cannot seem to get it to work.

    I am just looking to post the contents of a textinput into a .php file called data.

    The goal is to type in a number, post it with a "send" button, then view the contents of the data.php by clicking the "get" button. The person claimed it worked, but I am having no such luck :/

    His instructions were in text only, but if you would like to see: construct.net/en/forum/construct-2/general-discussion-17/basic-ajax-tutorial-135429

    Thank you for any help and insight!

    Tagged:

  • First thing I would check is if the target has CORS enabled.

    See the "Making AJAX requests cross-domain or in preview" section in the manual page.

    construct.net/en/make-games/manuals/construct-3/plugin-reference/ajax

    If CORS is not enabled (default), the server will block AJAX requests from anything that isn't hosted on the same domain/server.

  • I use these two lines at the top of php pages to circumvent the cross origin policies:

    header("Access-Control-Allow-Origin: *");

    header("Access-Control-Allow-Headers: *");

  • In your screenshot I see two things that could be an issue.

    1. You only really need the ‘post’ and not the request. You post the data to the php file, the php file executes and outputs any data that you want returned. That returned data will be in ‘Ajax.lastdata’. The way you currently doing it with two php files can work if preferred but you will still need to check the lastdata from the first post request in order to validate it was successful before doing the 2nd request.

    2. The post request appears to be sending the parameters via a GET, are you receiving them via GET or POST in the php file?

    If you use chrome, hit f12 and look at console & network, console will show any errors and network will show the two requests, the parameters and the returned data (if any)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you guys so much!

    I checked your suggestions and they were issues.

    Working now thanks :)

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