How do I request AJAX external json?

Get help using Construct 2

Post » Thu May 19, 2016 5:05 pm

Hello, I've managed according to the tutorials to use the local json file through the files folder. Now I would like to tryout hosted version of it.
I've tried to use the AJAX request www.site.com/file.json, but nothing worked, then I've tried to setup a php file index.php with the following contents
Code: Select all
<?php

echo date ("h:i:s");

?>


Just to try and output it, but nothing working so far.

What am I doing wrong?
B
11
S
3
Posts: 44
Reputation: 713

Post » Thu May 19, 2016 5:29 pm

try:
Code: Select all
<?php

header('Access-Control-Allow-Origin: *');

echo date ("h:i:s");

?>


How does Access-Control-Allow-Origin header work

Image

Image
Image
B
65
S
23
G
7
Posts: 1,505
Reputation: 10,896

Post » Fri May 20, 2016 2:26 pm

Thanks that worked!
B
11
S
3
Posts: 44
Reputation: 713

Post » Fri May 20, 2016 11:16 pm

..glad to hear..
StanBond wrote: I've managed according to the tutorials to use the local json file through the files folder. Now I would like to try out hosted version of it.I've tried to use the AJAX request http://www.site.com/file.json, but nothing worked...


so..let's say.. you upload json file on your site
Code: Select all
http://www.StanBond.com/gamefolder/file.json

and than you use
Image
in Preview(on localhost) this request will most likely fail & in console you will see something like
Image
you can bypass this ...with small PHP file(file.php)
Code: Select all
<?php
header('Access-Control-Allow-Origin: *');
$myfile = file_get_contents('http://www.StanBond.com/gamefolder/file.json');
echo $myfile;
 exit;
?>

upload this in your gamefolder
Code: Select all
http://www.StanBond.com/gamefolder/file.php

and now you request
Image


I really don't know if this is a good way....but works..
Image
B
65
S
23
G
7
Posts: 1,505
Reputation: 10,896

Post » Sat May 21, 2016 7:34 am

korbaach wrote:..glad to hear..
StanBond wrote: I've managed according to the tutorials to use the local json file through the files folder. Now I would like to try out hosted version of it.I've tried to use the AJAX request http://www.site.com/file.json, but nothing worked...


so..let's say.. you upload json file on your site
Code: Select all
http://www.StanBond.com/gamefolder/file.json

and than you use
Image
in Preview(on localhost) this request will most likely fail & in console you will see something like
Image
you can bypass this ...with small PHP file(file.php)
Code: Select all
<?php
header('Access-Control-Allow-Origin: *');
$myfile = file_get_contents('http://www.StanBond.com/gamefolder/file.json');
echo $myfile;
 exit;
?>

upload this in your gamefolder
Code: Select all
http://www.StanBond.com/gamefolder/file.php

and now you request
Image


I really don't know if this is a good way....but works..


I was also worried about the usability of this approach, and as in the most cases there is a need to read generated by the server json, I've decided to assign the json string onto the variable and echo it.

Code: Select all
<?php

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

$string = '{"c2array":true,"size":[3,10,1],"data":[[[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]],[["Nameless"],[1000],[100],[100],[10],[10],[50],[10],[10],[0]],[[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]]]}';


echo ($string);
?>
B
11
S
3
Posts: 44
Reputation: 713


Return to How do I....?

Who is online

Users browsing this forum: Cub3D, David181, yiuyiu6666 and 24 guests