0

I have created a JavaScript program for parsing XML using JQuery. Below is the code and sample XML. When running this, it is working fine with IE 8. But when I test in Chrome or FireFox, the $.parseXML does load xml. I am not seeing any errors, it just stops. I have put in alerts to see where it is stopping to determine the issue. Can anyone help? I don't understand why jQuery is working in IE 8 and not chrome or Firefox.

XML is simple

<?xml version="1.0" ?>
<userinfo>
  <rc>Y</rc>
  <un>George</un>
</userinfo>

or

<?xml version="1.0" ?>
<userinfo>
  <rc>N</rc>
  <un></un>
</userinfo>

Have created a JavaScript Parsing of XML using JQuery but only working in IE 8.

  function userLookup( v_page, v_uid, v_pwd )
  {
    var xmlHttp;

    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    } 
    catch (e)
    {
      // Internet Explorer
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
        try
        {
           xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
          alert("Browser does not support AJAX!");
          return false;
        }
     }
   }

   xmlHttp.onreadystatechange= function(){
     if(xmlHttp.readyState==4)
     {
        var xml = xmlHttp.responseText; 
        var xmlDoc = $.parseXML( xml );
        var $xml = $( xmlDoc );
        var $returncode = $xml.find( "rc" );

        if($returncode.text() == 'N')
      {
        alert("Invalid Userid/Password");
      }
      else if ($returncode.text() == 'Y')
      {
      document.getElementById('WelcomeMessage').innerHTML = "Welcome " + loginuser;
      document.getElementById('dialogLogIn').style.display = 'none';
      } else {
            alert( $returncode.text() );
          }
      }
    }

  xmlHttp.open("GET",v_page+"?v_uid="+v_uid+"&v_pwd="+v_pwd,true);
  xmlHttp.send(null);
}
Ben Lynch
  • 1
  • 1
  • Test it without the ajax. it works just fine for me: http://jsfiddle.net/Tentonaxe/66XFw/ || http://jsfiddle.net/Tentonaxe/66XFw/1/ debugging 101. Cut out the extra to figure out exactly where the error is occuring. It's not due to `$.parseXML` in this case. – Kevin B Oct 15 '13 at 21:32
  • I did as well, took out the call for xml and just put the xml in and it worked fine. I started playing with the xml file, I removed the from the xml return and it started working for me as well in the application as well. Will look into .ajax as well to get everything in jQuery, think it will be more stable that way too, thanks. – Ben Lynch Oct 15 '13 at 22:43

2 Answers2

1

You should probably use Jquery for the entire thing. Use $.ajax to send your requests see Jquery .ajax that way you don`t have to worry (in theory) about cross browser support. At the moment you are only using Jquery for parsing the XML and not for the actual requests, which may cause problems.

avrono
  • 1,648
  • 3
  • 19
  • 40
1

Its working fine!!! Try this,

Chrome/Firefox:

xml.children[0].childNodes[1].innerHTML;

IE8+/Safari:

xml.childNodes[0].childNodes[1].textContent;

IE8:

xml.documentElement.childNodes[1].text;

Sample code here,

var xml = $.parseXML(XMLDOC); 

Var xmlNodeValue = ""; 

if(userAgent.match("firefox") || userAgent.match("chrome")){ //Chrome and Firefox

xmlNodeValue = xml.children[0].childNodes[1].innerHTML;

}else{ // safari and IE8+

xmlNodeValue = xml.childNodes[0].childNodes[1].textContent; 

}
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87