How do I make AJAX work?

0 favourites
  • 7 posts
From the Asset Store
_______ Huge collection of metal fixtures ________
  • Hi,

    I'm struggling to get AJAX to work.

    I'm using the post command to send data to a PHP script I have on my localhost server, but no data gets sent.

    I read that I have to put this code somewhere for AJAX to be able to communicate with any domain, but I still don't know where to put this code: Access-Control-Allow-Origin: *.

    Can anyone please tell me, and preferably show me an example?

    Thanks!

  • Here is the right place

    <?php

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

    What script you use for post data?

    Maybe there is a problem.

  • Thanks for the response, i tried that but it didn't work. I am using Wampserver and managed to get it to work by enabling headers_module and by adding this code into httpd.conf:

    <IfModule mod_headers.c>

    Header set Access-Control-Allow-Origin: *

    </IfModule>

    But now i'm facing another problem, do you know how to get a respond from php script/website. I am able to successfully post data from Construct 3 to my php script and data gets stored into mysql database, this will be for registration, but i'm stuck on trying to do a login form.

    So i just have a text input field and a submit button, if the email exists in the php script you get logged in, but how can i get this response back to Construct 3 to tell it the email is valid. is it possible to us a echo command which says successfully registered and then that text gets sent back to Construct 3 and if it does the game can start.

    Hope you have some ideas?

    Thanks.

  • yes, use echo in the PHP script.

    that will get sent back to C3 - use AJAX On Complete for the "tag" you used, and the result will be in AJAX.LastData

  • Thanks again, appropriate it!

    Although i must be doing something wrong, this is what i have now?

    Maybe i have to do an event to compare the text from the php script and if it matches with 'success' i log in? Or maybe i can't put AJAX.LastData directly into a text field, maybe i have to encode it to a string first?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • that looks correct to me. I send a lot of data back and forth to a MySQL database. When I am sending multiple records I use rawurlencode in php and URLDecode in C2 on the AJAX.LastData, but that is to handle all the symbols that might be in the data.

    for simple messages, putting the LastData into a text field should work. so, either the text field isn't big enough to show the text, or the On Complete is not firing because of something with your server/php side of things.

  • Thanks, it works now.

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