how to access xml from server?

Get help using Construct 2

Post » Wed Aug 22, 2012 3:58 am

To access php files with ajax object stored in a server you need to add a "header access-control..." to the file.
But what about xml files, how do I access it?
Tryed but couldn't get the information of the file with AJAX and from what I know xml are just files that you storage information, so how do I request it from a webserver if there's no header in xml?Guif0DA2012-08-22 04:29:26
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Wed Aug 22, 2012 1:14 pm

Do you mean the 'Access-Control-Allow-Origin' HTTP header? That is only necessary if you are requesting a file from a different domain (e.g. requesting example.com/test.xml from scirra.com/demo.html). If you are making a cross-domain request, you need to set the server up to respond with that HTTP header - the file contents are not affected.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Wed Aug 22, 2012 1:38 pm

@Ashley I managed to request a picasa feed just with AJAX.
The astuce was to use yql (yahoo query language)
It seems http://query.yahooapis.com allows cross domain requests
you use it by requesting an url like that:
http://query.yahooapis.com/v1/public/yql?q=select * from xml where url="https://picasaweb.google.com/data/feed/base/user/112730361158490336509?alt=rss&kind=album&hl=en_US&imgmax=1600"
looks like an SQL query (:

I managed to make this prototype
https://dl.dropbox.com/u/23551572/C2-Games/Picasa/index.html

Depending on my motivation I might make an entire picasa viewer.

The only issue I had it that the xml object really don't like namespaces. It makes the XPath query fail.
I had to cleanup the xml and remove all the namespaced markup and attribute before loading it in the XML object.
I think I read that IE xml parser doesn't know how to deal with namespace, that might be why it's not yet implemented in C2 (?) but it would be great if at least there was a temporary action that would cleanup the namespace on load.
Anyway I managed to make it work (:
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Aug 23, 2012 12:03 am

[QUOTE=Ashley] Do you mean the 'Access-Control-Allow-Origin' HTTP header? That is only necessary if you are requesting a file from a different domain (e.g. requesting example.com/test.xml from scirra.com/demo.html). If you are making a cross-domain request, you need to set the server up to respond with that HTTP header - the file contents are not affected.[/QUOTE]
Hi Ashley thanks for reply
I have a webserver running on my pc and there's a file located like: http://localhost/test.xml

My game is running outside the 'https' directory from the webserver, in a normal folder on my PC and in preview, I used AJAX to request this test file but it can't access it, does nothing.
For php files I used whoever the header 'access-control' like you said and it works if I request with AJAX.
so my problem are with .XML files..
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Sun Aug 26, 2012 7:41 am

Sorry for bumping the post again, but I really need to understand well what's possible and not possible.
Also if there's another one with the same questions they might have some answers here..
I will explain with details what I'm trying and how I'm doing..


there is a webserver running in my PC, php files are inside the 'htdocs', however the game isn't in 'htdocs', it's in a local folder.
I can request php files with AJAX running in preview mode (adding the header to allow access), but I can't request .XML files. When I request a .xml file and add below a condition AJAX->On "xml request" Completed and add some simple actions to check if it has succeed to get it, nothing happens, meaning that AJAX failed to retrieve the file from the webserver, probably because of security definitions..

Now.. comes the question, so how do I successfully request a .xml file from a webserver?

I'm trying some solutions.. one of them is request a .php file with '(header('Access-Control-allow..')' and add "simplexml_load_file" to load the .xml file, then use the function 'asXML()' to send the whole xml information to AJAX.

example:
[code]
<?php
header('Access-Control-Allow-Origin: *');

$xml = simplexml_load_file("test.xml");
echo $xml->asXML();
?>
[/code]
After requested, load the ajax.lastdata to XML Object
this formula currently is working, but isn't the most efficient way...

Concluding, after all, is it possible to request("REQUEST URL") .XML files from a webserver using AJAX object? I already tried but couldn't succeed to request it, so if anyone know how to do it, please make a tutorial explaining..

Thanks
B
30
S
6
G
5
Posts: 433
Reputation: 6,061

Post » Wed Jun 08, 2016 1:15 am

Hi @Ashley I'd like to see a working example of this as well. I'm wasting too much time trying to get my project to work and all I want is to access an xml in my download folder in my online server from my C2 runtime test to adjust a game I'm working on. I don't want to include the xml files on the game otherwise I'd have to update the game for every little change on the xml.

EDIT: I solved my issue for testing purposes with a .htaccess file with the line Header set Access-Control-Allow-Origin "*"
placed in the folder with the file I want to access.
Freefall Bird
B
37
S
8
G
5
Posts: 262
Reputation: 5,360


Return to How do I....?

Who is online

Users browsing this forum: Lof and 4 guests