How do I get ajax to work

0 favourites
  • 13 posts
From the Asset Store
_______ Huge collection of metal fixtures ________
  • 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.

  • 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.

    <?php
    header('Access-Control-Allow-Origin: *');
    echo "Hi this is text";
    ?>[/code:37vdaafy]
    
    CORS means [i]Cross-origin resource sharing[/i]. 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)
  • 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

  • Something is amiss, when I inspect the response given by the GET call I get this :

    <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>[/code:3vn06igm]Is there some sort of encryption thingy on your server ?
  • 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?

  • Using my browser's dev tools, under the network tab.

  • What error do you get from the tutorial that i linked? i downloaded the example capx and even that doesnt work

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I get nothing when i use the empty tag

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

  • Funny, I tried with other browsers than IE11, and it's not working with them. The response is blocked because of the Access-Control-Allow-Origin only allowing "http://www.mywebbnerd.com". Why this works in IE and not in other browser is beyond me, I thought the CORS mechanism was server-side.

    edit: Ok, after a bit of reading, it seems that CORS is only for a user's safety... so I guess that it makes sense that it only works in IE. Turns out the Origin request is not set by IE (in my scenario at least), thus the Access-Control-Allow-Origin has no effect.

  • I just tried reaching your file with ajax and it seems that it actually is CORS that blocks the access. Since you included it in the file it is potentially overridden by the server.

    You can try to upload a htaccess file. Write the stuff below into a textfile, rename it to ".htaccess" (without "") and drop it next to your php files. Or ask the hoster. For testing, you can also disable the CORS in Chrome.

    <FilesMatch ".(php)">
    	Header set Access-Control-Allow-Origin "*"
    </FilesMatch>[/code:16q0ny3a]
    
    Or you can switch the hoster ofc  There´s some pretty cheap ones out there.
  • Switched host and it works like a charm, thanks guys!

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