AJAX via JSON to ARRAY

Get help using Construct 2

Post » Thu Jun 21, 2012 10:06 am

Hello everyone,

this is my first post here.
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:
http://www.scirra.com/forum/im-missing-something-with-the-ajax-object_topic53614.html

(i searched before the post was created...)Padman2012-06-21 10:20:50
B
7
Posts: 11
Reputation: 1,022

Post » Thu Jun 21, 2012 3:05 pm

Can you share your .capx with the output of mob_load.php just cut and pasted in to the 'load JSON' action?
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Thu Jun 21, 2012 3:08 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Thu Jun 21, 2012 4:21 pm

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 http://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 wont work. (i already tried to put the output directly in with double ", didnt 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 cant use a \' here!Padman2012-06-21 16:33:57
B
7
Posts: 11
Reputation: 1,022

Post » Thu Jun 21, 2012 4:37 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,630

Post » Thu Jun 21, 2012 4:50 pm

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 didnt 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
Padman2012-06-21 16:52:51
B
7
Posts: 11
Reputation: 1,022

Post » Thu Jun 21, 2012 9:09 pm

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 somewhenPadman2012-06-21 21:11:15
B
7
Posts: 11
Reputation: 1,022

Post » Thu Jun 21, 2012 9:52 pm

[quote]5. set text debug 2: array.at(1,1,1)[/quote]

The array index starts at 0. array.at(1,1,1) is outside the array, try array.at(0,0,0).
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Jun 22, 2012 6:49 am

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
B
7
Posts: 11
Reputation: 1,022

Post » Fri Jun 22, 2012 12:55 pm

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

str(array.at(0,0,0))
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Next

Return to How do I....?

Who is online

Users browsing this forum: Callan S, gamecorpstudio, getplay, Kagubro and 15 guests