I'm missing something with the AJAX object

Get help using Construct 2

Post » Thu Jun 21, 2012 9:49 am

From what I can tell, if I load an AJAX object, I can then use that data as a string, from AJAX.LastData. If it's in the correct JSON format, I can then load that string as a table array?

Is this correct?

I'm running a local server on localhost:82 (LAMP stack)


This is what I have now:

on start of layout -> load localhost:82/phpfile.php with tag "sometag"
on "sometag" completed -> load array vocab from ajax.LastData
                      -> set variable numberOfColumns from vocab.width

Here is the format of my JSON file - It does pass as valid JSON:

{"c2array":true,"size":[7,4,1],"data":[[["I"],["w?"],["?"]],[["you"],["n"],["?"]],[["him"],["t?"],["?"]],[["her"],["t?"],["?"]],[["it"],["t?"],["?"]],[["they"],["t?men"],["??"]]]}

Here is the .capx: http://dl.dropbox.com/u/12722403/flashCard.0.4.capx
B
12
S
3
G
4
Posts: 55
Reputation: 3,186

Post » Thu Jun 21, 2012 3:09 pm

The preview server runs on localhost:50000, which counts as a different origin to localhost:82, 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,949
Reputation: 178,544

Post » Fri Jun 22, 2012 1:39 am

The PHP file is part of a much bigger project - it pulls data from a database. The real URL is 2 folders deep.

This problem happens when I've uploaded the project to a folder on localhost:82. I've also tried it without the leading http://localhost:82 (only using /folder1/folder2/phpfile.php) I'll play around with project files, but I'd rather use files in my existing folder structure.
B
12
S
3
G
4
Posts: 55
Reputation: 3,186

Post » Fri Jun 22, 2012 6:47 am

OK...I've gotten it to work, but I can't figure out WHY. If I put a browser ALERT after the AJAX JSON load, all is well. If I take it out, the quiz skips to the end, and the grading button is shown. The capx file is at http://dl.dropbox.com/u/12722403/flashCard.0.5.JSON.capx
B
12
S
3
G
4
Posts: 55
Reputation: 3,186

Post » Mon Jun 25, 2012 1:25 am

The problem was with my timer - I didn't have the proper check to make sure it didn't start until after the JSON was loaded.
B
12
S
3
G
4
Posts: 55
Reputation: 3,186


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], luisgmolina, R0J0hound, Rezzon71, Yahoo [Bot] and 27 guests