mattward1979 Posted March 24, 2011 Share Posted March 24, 2011 Hey all, New here and VERY new to Ajax! (about 45 mins of experience...) I have a strange problem that I hope you can help with!! http://staffweb.cms.gre.ac.uk/~mk05/web/XML/3/ajax.html This link works fine with Ajax running on IE and FF, but EXACTLY the same script hosted here: http://stuweb.cms.gre.ac.uk/~wm815/ajax.html refuses to work in FF despite having access to the same XML file... The strange thing is that it works perfectly in IE! As I say I am very new to this, but for the life of me I cant see anything in the code that needs changing to get this working... Any help will be gratefully received! <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb"> <head> <title>JavaScript XMLHttpRequest AJAX Example</title> <style type="text/css"> table { margin-left:8em; } .centre { text-align:center; } .right { text-align:right; } </style> <script type="text/javascript"><!-- // Handle browser variations - thanks to jibbering.com function getHTTPObject() { var xmlhttp; /*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/ if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp; } // Create the HTTP Object var http = getHTTPObject(); // Create the XML Object var xmlDoc = null; // thanks to quirksmode.org for this if ( document.implementation.createDocument ) { xmlDoc = document.implementation.createDocument("", "", null); } else if ( window.ActiveXObject ) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); } else { alert('Your browser can\'t handle this script'); } // Send request asynchronusly function requestXML(url) { http.open("GET",url,true); http.onreadystatechange = handleHttpResponse; http.send(null); } // Event handler for the XML response function handleHttpResponse() { if (http.readyState == 4) { xmlDoc = http.responseXML; showIt(); } } // DHTML DOM gets the results into the page function showIt() { var output = '<table><tr><th> Location </th><th>Temperature</th></tr>'; var records = xmlDoc.documentElement.childNodes; for ( var i = 0; i < records.length; i++ ) { townName = records[i].firstChild.firstChild.nodeValue; tempValue = records[i].lastChild.firstChild.nodeValue; output += '<tr><td class="right">' + townName + '</td><td class="centre">' + tempValue + '</td></tr>'; } output += '</table>'; document.getElementById("data").innerHTML = output; } --></script> </head> <body> <h1>JavaScript XMLHttpRequest AJAX Example</h1> <p> <input type="button" onclick="requestXML('flatTemps1.xml')" value="flatTemps1.xml" /> <input type="button" onclick="requestXML('flatTemps2.xml')" value="flatTemps2.xml" /> <input type="button" onclick="requestXML('flatTemps3.xml')" value="flatTemps3.xml" /> <input type="button" onclick="requestXML('flatTemps4.xml')" value="flatTemps4.xml" /> <input type="button" onclick="requestXML('flatTemps5.xml')" value="flatTemps5.xml" /> </p> <div id="data"><table><tr><th> Location </th><th>Temperature</th></tr></table></div> </body> </html> Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted March 26, 2011 Share Posted March 26, 2011 Where is the "url" defined? that could possibly be part of the issue. Maybe its a hardcoded value for one site and not the other? grant it I only skimmed over your code but seeing as its working on one domain and not the other.. that would be my first guess. Also for firefox, I would suggest a plugin called firebug http://getfirebug.com/ incredibly useful for working with javascript on a page when its doing stuff in the background. You'll find out from that whats really tripping it up. I usually program on Firefox with the use of that, then when I am done with a piece cross check it on all the other browsers if all is good move on.. extra work yes but gotta have QA somewhere.. Quote Link to comment Share on other sites More sharing options...
mattward1979 Posted March 27, 2011 Author Share Posted March 27, 2011 Thanks for the advice! Started using firebug, and the error is: records.firstChild is null service = records.firstChild.firstChild.nodeValue; so it seems that the XML parsing is failing in everything but IE which is very strange.... Especially as this is 100% identical code to the working variant that is on the lecturers page! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.