PDA

View Full Version : Ajax Request: Parsing the Response Object



MaximusW
18 Aug 2011, 6:42 AM
I'm calling a web service using the following Ext.Ajax.request:



Ext.Ajax.request({
params: {CustomerNumber:customerNumber, SurveyID:surveyId},
url: 'http://mywebservicesite.com/survey.asmx/surveyInfo',
success: function(response) {
console.log(response.responseText);
},
error: function(msg) {
console.log("Error: " + msg);
}
})


The success outputs the following:


<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://mywebservicesite.com/survey.asmx">490</int>


So my question is how do I grab the value "490" from the response.responseText.

MaximusW
18 Aug 2011, 11:08 AM
I don't know if this is the best/easiest solution, but here's what I'm using:


parser=new DOMParser();
xmlDoc=parser.parseFromString(response.responseText, "text/xml");
x=xmlDoc.getElementsByTagName("int")[0];
y=x.childNodes[0];
console.log("Success: " + y.nodeValue);


I hope this may prove useful for newbies (like me).

fx-mike
19 Aug 2011, 1:17 AM
You could use Ext.DomQuery, here is an example


Ext.DomQuery.selectNode('int', response.responseXML).textContent

MaximusW
19 Aug 2011, 3:54 AM
You could use Ext.DomQuery, here is an example


Ext.DomQuery.selectNode('int', response.responseXML).textContent


That's exactly what I was looking for. Anytime you can reduce 5 lines of code to 1 line of code it's a good thing.
Thank you, fx-mike.