How do I request AJAX external json?

0 favourites
  • 5 posts
  • 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 , but nothing worked, then I've tried to setup a php file index.php with the following contents

    <?php
    
    echo date ("h:i:s");
    
    ?>[/code:3o6rnsop]
    
    Just to try and output it, but nothing working so far.
    
    What am I doing wrong?
  • try:

    <?php
    
    header('Access-Control-Allow-Origin: *');
    
    echo date ("h:i:s");
    
    ?>[/code:2iidrdkd]
    
    [url=http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work]How does Access-Control-Allow-Origin header work[/url]
    
    [img="http://lookpic.com/O/i2/1386/BZlh9KVj.png"]
    
    [img="https://media.giphy.com/media/3oEjHWiGFxTVinXClW/giphy.gif"]
  • Thanks that worked!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • ..glad to hear..

    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

    http://www.StanBond.com/gamefolder/file.json[/code:2ufvxgix]
    and than you use
    [img="http://lookpic.com/O/i2/997/i48LnMDP.png"]
    in [b]Preview[/b](on localhost) this request will most likely fail & in [url=https://developer.chrome.com/devtools#console]console[/url] you will see something like 
    [img="http://lookpic.com/O/i2/100/UXShPyYF.png"]
    you can bypass this ...with small PHP file(file.php)
    [code:2ufvxgix]<?php
    header('Access-Control-Allow-Origin: *');
    $myfile = file_get_contents('http://www.StanBond.com/gamefolder/file.json');
    echo $myfile;
     exit;
    ?>[/code:2ufvxgix]
    upload this in your [i]gamefolder[/i] 
    [code:2ufvxgix]http://www.StanBond.com/gamefolder/file.php[/code:2ufvxgix]
    and now  you  request
    [img="http://lookpic.com/O/i2/298/JDWRFZoK.png"]
    
    I really don't know if this is a good way....but works..
  • ..glad to hear..

    > 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

    http://www.StanBond.com/gamefolder/file.json[/code:6tp5f9fw]
    and than you use
    [img="http://lookpic.com/O/i2/997/i48LnMDP.png"]
    in [b]Preview[/b](on localhost) this request will most likely fail & in [url=https://developer.chrome.com/devtools#console]console[/url] you will see something like 
    [img="http://lookpic.com/O/i2/100/UXShPyYF.png"]
    you can bypass this ...with small PHP file(file.php)
    [code:6tp5f9fw]<?php
    header('Access-Control-Allow-Origin: *');
    $myfile = file_get_contents('http://www.StanBond.com/gamefolder/file.json');
    echo $myfile;
     exit;
    ?>[/code:6tp5f9fw]
    upload this in your [i]gamefolder[/i] 
    [code:6tp5f9fw]http://www.StanBond.com/gamefolder/file.php[/code:6tp5f9fw]
    and now  you  request
    [img="http://lookpic.com/O/i2/298/JDWRFZoK.png"]
    
    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.

    <?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);
    ?>
    [/code:6tp5f9fw]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)