I am newbie in javascript xml data handling. Currently, my ajax call to a server, and the server returns xml data, I don't know how to parse the xml data to get some values.
My ajax call:
$.ajax({
url: 'http://localhost:8080/someinterface/the-id',
type: 'GET',
async: false,
dataType: 'application/xml',
data: {id: 43},
success: function(data) {
//handle the data
},
error: function(xhr, status, error){
alert('error happens');
}
})
the returned xml data looks like:
<DOCUMENT>
<AGE>16</AGE>
<USERNAME>default user</USERNAME>
<SECURITYID>1111</SECURITYID>
...
how to parse the xml data to get for example "USERNAME" in my javascript ??
------------EDIT------------
I tried to use your proposed ways, but now I got error message:
XML Parsing Error: no element found Location: moz-nullprincipal:{120e8c1d-5174-4e94-9ebb-2bffda80b170} Line Number 1, Column 1: ^
and the ajax call result to the error function
-------------------PARTLY SOLVED------------------------
Finally, I found the reason is that my application is running on jetty server on localhost:8080, while my ajax is requesting xml data on tomcat server which is running on localhost:8085. Because of the domain port are different, I got the weired problem. Now, after I move my application to tomcat server and request tomcat server, I got the xml response successfully.
The ajax call goes to the success function, and I use
success: function(data){
xmlDoc = $.parseXML(data);
$xml = $(xmlDoc);
$userName = $xml.find('USERNAME');
var userName = $userName.text();
alert('1');
alert(userName);
}
I got alert('1') in my browser, BUT I do not get alert(userName) , I don't know why... So I put here "PARTLY SOLVED", so...why I do not get my userName alert..