...

View Full Version : JS & XML error



Jorgen
06-16-2009, 02:08 AM
I want to get the NWS xml data in html by using js.

My code is...


<script type="text/javascript">
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET","http://www.weather.gov/xml/current_obs/KNTU.xml",false);
xmlDoc.send("");
xmlDoc=xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("http://www.weather.gov/xml/current_obs/KNTU.xml");
}

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("observation_time_rfc822");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("weather")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("temperature_string")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>

My error is:


Line: 5
Error: Access is denied.

which is this part:


xmlDoc.open("GET","http://www.weather.gov/xml/current_obs/KNTU.xml",false);

Gjslick
06-17-2009, 12:48 AM
I believe that your problem is that JavaScript code cannot retrieve data from websites outside of the domain that the JavaScript code is running on. So, if your domain was www.something.com, the code would not be able to retrieve data from www.weather.gov (via XmlHttpRequest).

So, try this to see if this is the issue: Download that xml file and put it on your web server, and then change your code to point to the xml file on your web server. If it works in this case, then that is the problem.

What you can do to fix this issue is use php (or another server side scripting language) to download that xml file, and then your javascript can read from your own server. What you would do is have your javascript call a page like getWeatherData.php, and that php file would go out and retrieve the xml file from www.weather.gov, and return it to the calling javascript.

Any questions on this, post again and I'll be happy to help.

Greg

12 Pack Mack
06-17-2009, 01:35 PM
Jorgen:

See:

http://www.javascript-demos.com/Test/Weather.html

Jorgen
06-17-2009, 04:03 PM
Thank You but now i have a new problem:


Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618; yie8)
Timestamp: Wed, 17 Jun 2009 17:19:00 UTC


Message: Object required
Line: 26
Char: 3
Code: 0
URI: http://twccentral.co.cc/new_page_2.htm


To see my new code view the source of the URL.

Gjslick
06-17-2009, 08:23 PM
Well, first, just to let you know, it is much easier to develop on say, firefox instead of internet explorer. Firefox's error console gives you an actual error, instead of the stupid IE "object required" like you got there.

Firefox gives this actual error:

Error: x[i].getElementsByTagName("weather")[0] is undefined
Source File: http://twccentral.co.cc/new_page_2.htm
Line: 26


This is because it apparently is not finding that "weather" xml node. But the reason for this is because your xml file is not actually loading correctly. If you try to have your browser view the xml file directly, you'll see what I'm talking about. http://twccentral.co.cc/KNTU.xml

It is not finding the xslt stylesheet for the xml file. What you'd have to do is download the xslt stylesheet file that the xml file uses, put it in the same directory, and then change the xml file to point to the xslt file in the new location. However, this would be a crazy big hassle if you wanted to get up to date weather information.

The real solution is to use a server side script on your server that will retrieve the data for you, and wouldn't be restricted by the security limitations of javascript. Do you have any server side language installed? (like php, coldfusion, asp, jsp, ect.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum