AJAX Question

Discussion and feedback on Construct 2

Post » Thu May 04, 2017 2:58 am

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:

http://www.myserver.com/test/prueba.php?UUID=lolo

Could anyone advise?

Thank you.
B
8
S
1
Posts: 7
Reputation: 414

Post » Fri May 05, 2017 5:30 am

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.

i.e.
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)
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Sat May 06, 2017 8:08 pm

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!
B
8
S
1
Posts: 7
Reputation: 414

Post » Sat May 06, 2017 9:27 pm

In your PHP file, below <?php, add

i.e.

Code: Select all
<?php
header('Access-Control-Allow-Origin: *');

..rest of your code ...
?>
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Sat May 06, 2017 9:59 pm

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!
B
8
S
1
Posts: 7
Reputation: 414

Post » Sat May 06, 2017 10:07 pm

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 ?
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Sun May 07, 2017 8:47 pm

Hello,

Removing the question mark didnt seem to do anything :(

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
B
8
S
1
Posts: 7
Reputation: 414

Post » Mon May 08, 2017 6:09 am

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.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,592

Post » Tue May 09, 2017 3:55 am

Well, everything seems ok to me in the network tab, so i have no idea whats going on :|
Network Tab: http://imgur.com/a/o2SSV
B
8
S
1
Posts: 7
Reputation: 414

Post » Tue May 09, 2017 4:31 am

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:
http://imgur.com/a/T3eeY

However if i fill the data field with anything and adjust the URL field accordingly it just doesnt work...
B
8
S
1
Posts: 7
Reputation: 414

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 6 guests