2013-05-05 38 views
0
  1. 我是否需要将响应从servlet(xml)转换为xmlDoc,以便解析和检索某些值的 ?
  2. 如果是,那么下面的代码是否正确? console.log(id);打印一个函数,因此引发TypeError。如果没有,那该怎么办呢?
function xmlParser(xmlResponse) { 
    if (window.DOMParser) { 
     parser = new DOMParser(); 
     console.log(xmlResponse); 
     xmlDoc = parser.parseFromString(xmlResponse, "text/xml"); 
     console.log(xmlDoc); 
    } 
    id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue; 
    console.log(id); 
    key = xmlDoc.getElementsByTagName("passkey")[0].childNodes[0].nodeValue; 
    console.log(key); 
    return format(id, key); 
} 

回答

0

不,你不需要转换的响应,因为你可以通过responseXML财产得到xmlDoc直接。

例子:

xmlDoc = xmlResponse.responseXML; // you'll probably need to change it because I don't know what is value of xmlResponse in your case 
id = xmlDoc.getElementsByTagName("id")[0].childNodes[0].nodeValue; 
//and so on... 
+0

的内容类型从服务器的响应必须是“text/xml”的或你的responseXML属性将是无效的。 – Ben 2013-05-05 10:39:46

+0

我删除了转换代码。我从servlet发送xml。但仍然得到'Uncaught TypeError:无法读取属性'childNodes'undefined' – John 2013-05-05 11:10:34

+0

我的坏...它解决了 – John 2013-05-05 11:20:53