How do I load a txt file from my server

Get help using Construct 2

Post » Wed Apr 12, 2017 4:33 am

Hi guys. I was reading the manual about AJAX and I didn't get how exactly I should allow cross-domain access so I can load text and image files from my server.

This "Access-Control-Allow-Origin: *" is used on the server?


Making AJAX requests cross-domain or in preview
By default, browsers block AJAX requests across domains. This means, for example, a game on scirra.com can request other pages on scirra.com, but cannot request pages on facebook.com. This is an important security feature of web browsers (it is not specific to Construct 2 or its AJAX object).

Also, when previewing in Construct 2 the game runs on localhost. This counts as a different domain to the rest of the internet, so typically AJAX requests to any web page will fail during preview, unless the server explicitly allows cross-domain requests.

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: *

This will enable AJAX requests from any domain, but you should still be aware of the possible security implications of this. For more information on cross-domain requests see HTTP access control on MDN.



Could someone help me?

Thanks.
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Wed Apr 12, 2017 6:11 am

"Access-Control-Allow-Origin:" Is a setting on your web server (Apache?) where the file you are trying to get is located. If you not managing your own web server config, such as with a third party hosting site, you might not have access to this. You can always try inquiring with your host/server admin to see if it can be enabled.

ref: https://enable-cors.org/server_apache.html
Mistakes were made.
B
52
S
26
G
108
Posts: 1,605
Reputation: 61,131

Post » Wed Apr 12, 2017 7:43 am

@oosyrag So if I have the access can I Just do:
<?php
header('Access-Control-Allow-Origin: *');

'Whatever text I want goes here'
>

And so using the php file as a normal text file. Would work?
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Wed Apr 12, 2017 4:10 pm

I have no idea. Try it and find out.
Mistakes were made.
B
52
S
26
G
108
Posts: 1,605
Reputation: 61,131

Post » Wed Apr 12, 2017 8:08 pm

I'd create a single PHP script that fetch text files and output them. So you don't have to create a new PHP file with "header('Access-Control-Allow-Origin: *');" everytime.

Also, if the application is hosted on the same server, none of this is necessary.
B
71
S
30
G
24
Posts: 984
Reputation: 19,003

Post » Wed Apr 12, 2017 8:42 pm

@Magistross The problem is that I have no knowledge at this.

What I need to do is: everyday I'll update a repository in this server with text and images. I thought about allowing the access to this entire repository because I'm making an app(NW.JS) that will be running in another domain and will get these texts and images.

Is it simple to do? Could you make an example for me or link some documentation so I could try to do it? ^^
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Wed Apr 12, 2017 9:44 pm

@Magistross I managed to do it using 000webhost. I uploaded a txt file and changed the permission to read and write for "others". Everything done using their layout. Is it trustable? :P
B
56
S
21
G
3
Posts: 602
Reputation: 6,612


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, Uk4dee, Yahoo [Bot] and 23 guests