XML & Javascript parser

R

rischfre

Hi

In order to build a weather prediction page, I was thinking to write
down a Javascript (on the client). This javascript should contact a
server (www.live.com) in ordert to extract the weather forecast.

In summary, I want a javascript based on the html client (internet
Explorer), creating a connection and extractign the data.
This sounds simple but for whatever reason, I cannot manage to extract
any data of this XML page.

Perhaps can you help me?

This code you can past it, as a document by it s own and test it and
complete it for your own pages (the server is ready to answer you).

thanks


<script language="javascript">
<!--

// Server pages - Parameters.
// URL = <Debut>+CodeVille+<Fin>
var Debut =
"http://www.live.com/cfw/weatherdata.aspx?wealocations=fr:";
var Fin = "&weadegreetype=C&culture=fr-fr&rand=0.0";


// Objects initialisation
function Weather(code)
{
//Path
this.Code = code; //town code (key)
this.LocationName = null; // Full name of the town
this.URL = null; //path for icons
this.ImageRelativeURL = null; // second part of the path

//Weather of the current day
this.CurrentTemperature = null;
this.Skycode = null; //icon
this.SkyText = null; //description
this.Date = null; //date
this.Day = null; // day of the week
this.Time = null; // time
}

// LThis is the function which has the problem
Weather.prototype.GetData = function()
{
// Object creation
var xmlData = new ActiveXObject("Microsoft.XMLDOM");
xmlData.async=false;

// URL
var URL = Debut + this.Code + Fin;

// Check the URL
alert(URL);

// Connection to the XMLwebpage
xmlData.load(URL);

// Check the connection by displaying any data
alert(xmlData.documentElement.childNodes(1).text); // <---- Here
is the error. I tryed with and without 'documentElement', but this does
not work

//read data...
alert(xmlData.root.children.item("weatherdata"));
}


// Strasbourg is the town I want to have. initialisation of the Data
var Strasbourg = new Weather("FRm_Strasbourg");

// Strasbourg object receive the data.
Strasbourg.GetData();

-->
</script>
 
J

Jonas Raoni

In order to build a weather prediction page, I was thinking to write
down a Javascript (on the client). This javascript should contact a
server (www.live.com) in ordert to extract the weather forecast.

You can't access resources out of your domain name. So you'll need to
make a proxy.

"download.abc?uri=http://www.live.com"
 
R

RobG

That s strange, is it a security restriction of Internet Explorer ?

It is a restriction of all browsers. XMLHttpRequests can only be made to
the domain that served the page containing the request. There are
significant security issues in doing otherwise.

You can get around that with JSON and dynamically created script elements,
provided the server you want to connect to supports it.

<URL:http://borkweb.com/story/look-ma-cross-domain-scripting>


Look for weather servers that support JSON or JSONP.
 
C

Csaba Gabor

RobG said:
It is a restriction of all browsers. XMLHttpRequests can only be made to
the domain that served the page containing the request. There are
significant security issues in doing otherwise.

Specifically, if cross domain XMLHttpRequest was possible, and you
happened to visit a malicious site, what it could do is to simulate a
page submission sequence in your name. That is to say, from the point
of view of that remote domain, it is getting a request from the browser
so as far as it can tell, the browser's user is the one making the
request (Note: one off cross domain requests are available using
I/FRAMEs, but you can't get at them).

Csaba Gabor from Vienna
You can get around that with JSON and dynamically created script elements,
provided the server you want to connect to supports it.

<URL:http://borkweb.com/story/look-ma-cross-domain-scripting>

See also: http://www.json.org/
http://developer.yahoo.net/common/json.html
 
J

Julian Turner

Thank you very much for your help, and your precious information!

One other thought. If you are making this page just for yourself, and
you are working on the Windows platform, you can save the web page with
the extension ".hta" : I think it should then run the web page with
fewer security restrictions.

Regards

Julian Turner
 
G

google

In order to build a weather prediction page, I was thinking to write
down a Javascript (on the client). This javascript should contact a
server (www.live.com) in ordert to extract the weather forecast.
In summary, I want a javascript based on the html client (internet
Explorer), creating a connection and extractign the data.
This sounds simple but for whatever reason, I cannot manage to extract
any data of this XML page.
Perhaps can you help me?

Hello,

As someone mentioned already, using an AJAX-style callback would
probably work best for this kind of thing. I put together a really
simple AJAX example script, in case you need somewhere to start:

http://www.impliedbydesign.com/super-ajax-programming-seed.html

I hope that helps. Good luck!

Chris S.

Free Web Design Tools
http://www.impliedbydesign.com/free-software-scripts.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top