Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: JS & XML error

  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JS & XML error

    I want to get the NWS xml data in html by using js.

    My code is...

    Code:
    <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:

    Code:
    Line: 5
    Error: Access is denied.
    which is this part:

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

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    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

  • Users who have thanked Gjslick for this post:

    Jorgen (06-17-2009)

  • #3
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts

  • #4
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank You but now i have a new problem:

    Code:
    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.
    Last edited by Jorgen; 06-17-2009 at 07:20 PM.

  • #5
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    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.)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •