Fengist's Forum Posts

  • Ok, that's a start.

    If you see an IP in the httporigin.log then that means that it's being called from somewhere outside of your domain and I'd be surprised if it gave a response. What you should see is something like this:

    preview.construct.net

    mydomain.com

    My next question. Do you have an HTTPS certificate on the site you're trying to access?

  • If it works, then it's all good.

    Years ago when programmers had to write code for Commodore 64's they had to be super efficient. They had to write elegant code in order to make it fit in 64k of ram and they had to make it elegant in order to run quickly on an 8 bit CPU.

    Today, programmers have a LOT more freedom to write functional code rather than elegant.

    The questions you need to ask yourself as to whether your method is the best.

    1. Does the code work as intended?

    2. Can you follow how the code works?

    3. Is this code for public consumption or just for internal use?

    Does the code do what you want, does it do it quickly enough, does it not gobble an excess of ram... is it functional?

    The only time I get concerned with elegance is if I'm writing code for someone other than myself who also understands how to write code. In that case, I try to make it elegant so they don't think I'm a total noob. For my own use, I comment my code so I can come back later and figure out "what the hell was I thinking."

    In all honesty, the vast majority of your end users won't give a damn about code elegance. What they will give a damn about is how it looks, is it fast and does it contain bugs.

  • You do not have permission to view this post

  • Your English is just fine.

    Yea, it's pretty annoying not to see what the actual AJAX error is.

    If you read further down that post above there's some code to log incoming requests to file so you can actually see if the AJAX requests are coming through. That would be my first test to see if the various devices are even making the requests and if they are, do they have an origin.

  • Thanks Ashley ! I think the JS noob gets it! This is really cool, being able to call a custom JS function inside a layout.

    This works by calling skillStart() on start of layout and skillStop() on end of layout (to prevent it from creating multiple intervals) and basically, (along with some CSS) pulses a progress bar between two values.

    function anipgbar() {
     var p = document.getElementsByClassName("animated");
     var orig = parseInt(p[0].getAttribute('data-orig'));
     var cval = parseInt(p[0].getAttribute('value'));
     if (cval > orig) {
     p[0].setAttribute('value', orig);
     } else
     {
     p[0].setAttribute('value', orig + 1);
     }
    };
    
    var drawInterval;
    
    function skillStop() {
     clearInterval(drawInterval); 
    };
    
    function skillStart() {
     drawInterval = setInterval(anipgbar, 2000);
    };
    
  • And here's a little trick you can use to see if the AJAX call is actually going through

    replace

    if (isset($_SERVER['HTTP_ORIGIN'])){
     $http_origin = $_SERVER['HTTP_ORIGIN'];
    }
    else
    {
     die();
    }
    

    with this:

    $myfile = fopen("httporigin.log", "a") or die();
    
    if (isset($_SERVER['HTTP_ORIGIN'])){
     $http_origin = $_SERVER['HTTP_ORIGIN'];
     fwrite($myfile, $http_origin."\n");
     fclose($myfile);
    }
    else
    {
     fwrite($myfile, $_SERVER['REMOTE_ADDR']."\n");
     fclose($myfile);
     die();
    }
    
    

    What this will do is create a .txt file in the directory where this script is called httporigin.log.

    If an incoming request has an origin, it will add that url to the text file. If it doesn't have an origin, it will instead log the ip address.

    This is real handy for determining where your AJAX requests are coming from.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Right, I believe it's like I said. I seem to recall reading recently that the C3 AJAX specifically looks for the HTTPS, something I don't think C2 did.

    I know if I change the URL's in that script above to http that it won't work.

  • Just posted this reply elsewhere. See if this fixes it:

    construct.net/en/forum/construct-3/general-discussion-7/ajax-changed-recently-not-140404

  • Oh, and once you have it all running and working, delete the two lines of error reporting...

  • First of all, the

    Access-Control-Allow-Origin: *

    is a huge security hole. It means, anyone, anywhere can run that php file and can Post, Get, do whatever they wish to it. The whole point of CORS and AJAX is to limit who can call it and what they can do.

    Try this code instead:

    error_reporting(-1); // reports all errors
    ini_set("display_errors", "1"); // shows all errors
    
    if (isset($_SERVER['HTTP_ORIGIN'])){
     $http_origin = $_SERVER['HTTP_ORIGIN'];
    }
    else
    {
     die();
    }
    
    header('Access-Control-Allow-Methods: "POST"');
    
    if ($http_origin == "https://preview.construct.net" || $http_origin == "https://www.mysite.com") {
     header("Access-Control-Allow-Origin: $http_origin");
    }
    

    Change 'mysite' to the URL of your website.

    This is a chunk of code I save as 'header.php' and

    include "header.php"
    

    in all of the php files I want to access with AJAX.

    Access-Control-Allow-Origin: $http_origin

    determines who can access the script and

    Access-Control-Allow-Methods: "POST"

    determines what they can do.

    The first two lines are for error reporting so you should see if the script crashes.

    Now, that being fixed.

    If you are still running http and not https that's likely the issue.

    Chrome in particular will block access from a secure site to an insecure one. You'll notice this because a little shield icon will appear in the top right of Chrome. When you hover over it, it will tell you that the site is trying to run insecure scripts and it blocked it. You can click on that shield and unblock the site but you have to do it each and every time.

    Finally, I recall reading somewhere recently that AJAX blocked requests to non-https sites. If you haven't gotten your certificate yet, now is a good time.

    If you need a secure website, I personally use interserver.net. $5.00 a month and that includes a free cert.

    That's my guess.

  • or 175+random(0,20)

    newt showoff...

  • Elegant is a word used by those who get paid entirely too much to sit and think of ways to make science appear attractive to the artistically inclined mind. For the rest of us, functional replaces elegance.

    LayoutName = "X" -> Call Y

    If it ain't broke, don't fix it.

  • In a very general sense:

    Have your enemies raycast toward the player.

    If the player is hit with the raycast, store the x,y location of the player in that enemy's instance variables and shoot.

    If the raycast then misses the player, have them move toward the x,y location and raycast again.

    If that raycast misses, you can have them start wandering around and searching or return to their original location.

  • I'm not positive but I don't think the arcade is set up to use C3, only C2. Last I read they were planning on reworking the arcade.