How do I get ajax to work

Get help using Construct 2

Post » Tue Jun 20, 2017 11:38 am

Im trying to follow this tutorial and i have downloaded the examples but it simply does not work

Anyone know why?

https://www.scirra.com/tutorials/61/aja ... t-2/page-4

I have tried both the in tutorial example and my own of setting this up

http://gametesting.rf.gd/example.php

and then making an ajax request (tag : "" Url: "http://gametesting.rf.gd/example.php")
get nothing on completed, only on error.
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Post » Tue Jun 20, 2017 2:34 pm

It´s hard to tell without seeing the source code. What error do you get? I work a lot with ajax and never had much of a problem with it. The only issue I had were CORS issues, that were relatively easy solved. I think that might be your problem. Try the following php code.

Code: Select all
<?php
header('Access-Control-Allow-Origin: *');
echo "Hi this is text";
?>


CORS means Cross-origin resource sharing. Since C2 preview opens it´s own server that then tries to connect to your server, your server says "An outside connection... boy that might be harmful to allow access" and thus denies it. If you export your project as html and upload it on your webspace, it should work without defining the header, but from outside sources you have to specifically allow access.

Please be aware that "header('Access-Control-Allow-Origin: *');" opens the access to the file up from any source outside your own server (* is the wildcard) and is not recommended. For testing it´s totally fine though. Instead of * you can also use any webadresses (including localhost and/or the ip of the construct preview)
B
16
S
3
Posts: 69
Reputation: 1,048

Post » Tue Jun 20, 2017 2:57 pm

Still cannot get it to work, i uploaded the php as example php exactly as you described

http://gametesting.rf.gd/example.php <--- theres the php, i just uploaded what you suggested

And here is the code

Image
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Post » Tue Jun 20, 2017 3:30 pm

Something is amiss, when I inspect the response given by the GET call I get this :
Code: Select all
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("53d32dc6fe5278a3017bf7fc9b79b902");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://gametesting.rf.gd/example.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
Is there some sort of encryption thingy on your server ?
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Tue Jun 20, 2017 3:36 pm

I just tried to use a free server to do this, just going through tutorials and learning, perhaps i'll try another. How did you manage to get that response Magistross?
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Post » Tue Jun 20, 2017 3:43 pm

Using my browser's dev tools, under the network tab.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Tue Jun 20, 2017 3:45 pm

What error do you get from the tutorial that i linked? i downloaded the example capx and even that doesnt work
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Post » Tue Jun 20, 2017 3:59 pm

The file ajaxexample1_1.capx is flawed. The second event "On AJAX.LastData completed" doesn't make any sense. If I replace it by the trigger "On any completed" or use the empty tag, it works.
B
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » Tue Jun 20, 2017 4:27 pm

I get nothing when i use the empty tag
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Post » Tue Jun 20, 2017 5:07 pm

So you say this worked for you on the tutorial example 1 capx?

Image
B
23
S
11
G
4
Posts: 69
Reputation: 3,439

Next

Return to How do I....?

Who is online

Users browsing this forum: tarek2 and 19 guests