AJAX changed recently? Does not load textfile anymore

0 favourites
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • Hi. My old project loads a text to display news and works fine but my new project does not load the file from my server. Is it possible to get a more detailed error message for Ajax actions? On error triggers but what's the error?

    I checked that the server has the proper.htaccess with allow origin * like for my old c2 project. I uploaded it as utf8 and ANSI file and checked chmod etc but I can't figure out why it does not load the textfile (just 1 line of text). Any ideas? 🤔 It's hosted on a 1&1 Server.

  • have you got a secure server Https ?

  • no, its http. does c3 require https?

    I tried loading the file from the folder of my old c2 project and it shows error. so it seems to be a c3 issue and not server related.

  • no, its http. does c3 require https?

    I tried loading the file from the folder of my old c2 project and it shows error. so it seems to be a c3 issue and not server related.

    When previewing a file from a server it does.

    Although when it is exported it should work.

  • Thank you very much! totally forgot this, did not use ajax for some time. Exported NW.js worked, hope its the same for Win10 UWP and Android.

    but I just got my included ssl certificate and should be able to use SSL in the future for my apps. :)

  • in order to bypass the ssl thing just type your adress without http in front... like

  • I tried with and without and just removed http but it does not show up in preview. :(

    from the help file: If you want AJAX requests to your server to work from any domain, or in preview, you can configure it to send the following HTTP header:

    Access-Control-Allow-Origin: *

    I added this but it does not work in preview.

  • I tried with and without and just removed http but it does not show up in preview. :(

    from the help file: If you want AJAX requests to your server to work from any domain, or in preview, you can configure it to send the following HTTP header:

    Access-Control-Allow-Origin: *

    I added this but it does not work in preview.

    try this tutorial ajax in local preview localhost 50000 there are many like it. it seems is a browser access issue. it might also be your firewall ports.

    even if its an old tutorial and is for C2 it should work for C3 as well, i don't think the principle that ajax works on, has changed yet.

  • Hi. It works fine in my old project but in the new one it's broken again.

    Its the same server, just another directory. It has the same .htaccess as the folder where it works. (with the allow origin * line) But it does not work. Any ideas why? I tried changing access rights for the file, changing .json to .txt but its not loading. :(

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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks. I got a SSL cert but could not install it on my 1&1 Server yet. The instructions they linked were confusing and the config files mentioned were not there. Still trying to figure it out.

    Right now I only use the AJAX plugin to load text files from my server to load news headlines or other information text. But I want to use PHP for some MySQL actios soon. (get data from DB or post data)

    I will take a closer look at your instructions and try to implement it.

    But I dont understand why it seems to work fine on the other project. Its http://, same server, same .htaccess, both load a text file. Its just one line On Start of Layout > Get from URL and then using the returned string to display as text or this time I tried json data for loading a save state.

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

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

  • My response to the same problem from another guy:

    construct.net/en/forum/construct-3/general-discussion-7/construct-ajax-crossdomain-144008

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