AJAX via JSON to ARRAY

0 favourites
  • 14 posts
From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • Hello everyone,

    this is my first post here. <img src="smileys/smiley1.gif" border="0" align="middle" />

    I am testing arround with Ajax and JSON (I used both techniques before).

    My objective is simple, receive some data from a database, create new object, set some variables.

    Well... the LOAD function is not quite specified in the manual, so maybe i have a simple syntax error. Here we go:

    1. Every 5 Seconds: Ajax Request "http://localhost/mob_load.php"

    2. mob_load.php's output:

    {"c2array":true,"size":[3,2,1],"data":[ [ [300],[25] ], [ [100],[50] ], [ [800],[10] ] ]}

    3. Ajax Request finished: Load from JSON string(ajax.lastdata)

    4. Set Text debug 1: ajax.lastdata (just to check if the ajax request is fine)

    5. set text debug 2: array.at(1,1,1)

    Everything is working fine, except of step5. Nothing happens.

    May someone can help me with this. :)

    Thanks in advance,

    Pad

    *EDIT*

    I saw that this post is almost identically to mine:

    scirra.com/forum/im-missing-something-with-the-ajax-object_topic53614.html

    (i searched before the post was created...)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Can you share your .capx with the output of mob_load.php just cut and pasted in to the 'load JSON' action?

  • Oh, just realised: the preview server runs on localhost:50000, which counts as a different origin to localhost:80, so the browser will block the request due to the same-origin policy. See the section on cross-domain requests in the AJAX documentation.

    Also, setting up a separate PHP server to serve a simple JSON file is unnecessary. You can do it by using project files.

  • Hi Ashley,

    thanks for your response.

    I already read the documentation.

    I want to read something from a database and put it into an array in construct, so i can`t use a simple file in my project folder, right?

    The server is already configured, so this is no problem. Ajax requests are working fine. If i open localhost/project/index.html i can use the ajax funtcions and receive the data. Here a screen, i putted the ajax.lastdata in the text box:

    https://www.dropbox.com/s/9s6jl2ss782gs3w/ajax_form.PNG

    The problem is that the json load function won?t work. (i already tried to put the output directly in with double ", didn?t work either).

    I uploaded the file here:

    Link Capx

    only the mob_load ajax function is neccessary. the rest is testing stuff.

    the output from the mob_load.php is:

    {

    "c2array":true,

    "size":[3,2,1],

    "data":[      [ [300],[25] ],

         [ [100],[50] ],

         [ [800],[10] ]

    ]}

    hope this will help.

    thanks and regards

    pad

    ps. didn`t know that u can?t use a \' here!

  • The JSON you pasted there is wrong, it says the size is 3 x 2 x 1 but the array elements in "data" are sized 1 x 3 x 2. Did you type it manually? The Array object shouldn't have given you anything that looks like that.

  • yes, this one is manually created... i used some examples (found very few) from here for that.

    my first try was to generate it with php and json encode. but this didn?t work either, so i tried it manually.

    the json encode gives me: [{"hp":300,"speed":25},{"hp":100,"speed":50},{"hp":800,"speed":10}]

    phpcode:

    $mob[] = array('hp' => 300,'speed'   => 25);

    $mob[] = array('hp' => 100,'speed'   => 50);

    $mob[] = array('hp' => 800,'speed'   => 10);

    echo json_encode($mob);

    is there any kind of dokumentation of this special format? :(

    regards

    pad

  • Until anyone has a good idea i helped me with tokenat().

    I have to do many more requests with ajax, but this one is working fine.

    in my case i created this string:

    $string = '3;100;20;200;10;50;40';

    the first value is the count of mobs and behind n-variables.

    then for i <= int(tokenat(ajax.LastData,1,";"))

    set hp = int(tokenat(ajax.LastData,loopindex,";"))

    set speed = int(tokenat(ajax.LastData,loopindex+1,";"))

    hope this will help someone somewhen

  • . set text debug 2: array.at(1,1,1)

    The array index starts at 0. array.at(1,1,1) is outside the array, try array.at(0,0,0).

  • hi ramones, did not work.

    just to verify, that is what i have done:

    step one:

    created array in construct with properties:

    width: 1

    height: 3

    depth: 2

    step two:

    loaded this string via ajax:

    {

    "c2array":true,

    "size":[1,3,2],

    "data":[      [ [300],[25] ],

         [ [100],[50] ],

         [ [800],[10] ]

    ]}

    as i understand now the size is similar to the array in construct, so

    • "size":[width,height,depth] - correct?

    step 3

    use the function Load from JSON string with ajax.lastdata (or the string itself)

    step4

    set debug.text to array.at(0,0,0)

    array.at(0,0,0) should be 300, right?

    If i get this thing working i will write a tutorial about it -.-'

    otherwise i have to fill my array with a splitted string...

  • Right, the textbox requires text and the array contains numbers. Try setting it to

    str(array.at(0,0,0))

  • the result in the text box is 0 if i transform it to a string, not 300.

    Is there any way to get a log or debug information?

  • Hi,

    i got the same Problem. If i convert it to String there is a 0 displayed not the numbers inside.

    There must be an answer for that.

    Kind Regards

    MCSell ;)

  • I know it is an old post, but looking for an answer I came here.

    I could resolve it, the Json format its a must.

    My php to fill a 2 dimension array:

    $data is an asociative array, each row has an array with 3 key => value data: id:number, name:string, points:number. It's for a ranking.

        $rows = count($data);
        $cols = 3;
        $depth = 1;
        if (!empty($data)) {
            $datos = '{"c2array":true,"size":[' . $rows . ',' . $cols . ',' . $depth  . '],"data":[';
    
            foreach ($data as $fila) {
                $datos .= "[";
                foreach ($fila as $key => $value) {
                    $datos .= '["' . $value . '"],';
                }
                $datos = rtrim($datos, ",");
                $datos .= "],";
            }
            $datos = rtrim($datos, ",");
            $datos .= "]}";
        }[/code:280juy0m]
    
    [code:280juy0m]{
        "c2array":true,
        "size":[3,3,1],
        "data":
        [
        [
            ["4"],["mark"],["299"]
        ],[
            ["2"],["steve"],["23"]
        ],[
            ["3"],["bill"],["3"]
        ]
        ]
     }[/code:280juy0m]
    
    Then, you can access all data in array.
    Name: arrRanking.At(X,1,0)
    Points: arrRanking.At(X,2,0)
    
    I tried to loop filling 10 text boxes but I don't know how... yet jiji.   
    
    UPDATE: Now I could fill the text boxes in a loop.
    
    You only need a instance variable in the text box.
    
    [img="https://4.bp.blogspot.com/-95R8o9WE-Vw/Vi99up6hstI/AAAAAAAAA24/Wh3dYTs4rqY/s1600/json%2Bconstruct2.PNG"]
    
    Alek
  • hi ramones, did not work.

    just to verify, that is what i have done:

    step one:

    created array in construct with properties:

    width: 1

    height: 3

    depth: 2

    step two:

    loaded this string via ajax:

    {

    "c2array":true,

    "size":[1,3,2],

    "data":[ >      [ [300],[25] ],

         [ [100],[50] ],

         [ [800],[10] ]

    ]}

    as i understand now the size is similar to the array in construct, so

    - "size":[width,height,depth] - correct?

    step 3

    use the function Load from JSON string with ajax.lastdata (or the string itself)

    step4

    set debug.text to array.at(0,0,0)

    array.at(0,0,0) should be 300, right?

    If i get this thing working i will write a tutorial about it -.-'

    otherwise i have to fill my array with a splitted string... Padman2012-06-22 06:50:08

    Unless i misunderstood (which is often) If you set your json to "size":[3,2,1] instead of "size":[1,3,2] it should work, it gets 300 from array.At(0,0,0), therefore array.At(2,1,0) = 10 I have provided a capx here.

    http://www.sizzle-games.com/Ajaxtestarray.capx

    Also if you set your file to

    {

    "c2array":true,

    "size":[3,2,1],

    "data":[ [ ["cat"],[25] ],

    [

    [100],[50] ], [

    [800],[10] ]

    ]}

    array.At(0,0,0), will return cat

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