Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

process xml soap response 1

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
Hello all im trying to consume a web service using javascript and soap but i cant seem to figure out how to getthe response,

Any help with this is greatly appreciated im at a loss on it

Here's what the soap response looks like
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi=" xmlns:xsd=" xmlns:soap12=" <soap12:Body>
<hintResponse xmlns=" <hintResult>string</hintResult>
</hintResponse>
</soap12:Body>
</soap12:Envelope>


Here's the Code
<script type="text/javascript" language=javascript>

var xmlHttp;
var strEnvelope = "<?xml version='1.0' encoding='utf-8'?>"+
" <soap12:Envelope xmlns:xsi=' +

" xmlns:xsd=' +

" xmlns:soap12=' +

" <soap12:Body>" +

" <hint xmlns=' +

" <Term>b</Term></hint>" +

" </soap12:Body>" +

"</soap12:Envelope>";

function showHint(str)
{

if (str.length > 0)
{
var url="xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open('POST', url , true);
xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8")
xmlHttp.send(strEnvelope);
xmlHttp.onreadystatechange = checkState;
}
}
function checkState(){
if(xmlHttp.readyState=="complete"||xmlHttp.readyState==4){
placeHint(xmlHttp.responseXML);
}
else{
alert("Not Complete");
}
}
function placeHint(xmlresponse) {
alert(xmlresponse);
response = xmlresponse.documentElement;
method = response.getElementsByTagName('hintResponse')[0].firstChild.data;
result = response.getElementsByTagName('hintResult')[0].firstChild.data;
}
</script>

I receive [object] in the alert inside of the placeHint function but then receive the error that response is null or not an object. Im not even sure if this is the best method for getting the data. Like i said id appreciate any insight


<% ASP Web App Development %>
 
Try this.
[tt]
function placeHint(xmlresponse) {
alert(xmlresponse);
xmlresponse.setProperty ("SelectionNamespaces","xmlns:y=' result=xmlresponse.selectSingleNode('//y:hintResponse').text;
//method? I don't see it.
}
[/tt]
 
Amendment
I meant hintResult instead of hindResponse. A re-take.
[tt]
function placeHint(xmlresponse) {
alert(xmlresponse);
xmlresponse.setProperty ("SelectionNamespaces","xmlns:y=' result=xmlresponse.selectSingleNode('//y:hint[blue]Result[/blue]').text;
//method? I don't see it.
}
[/tt]
 
Thanks for your post! I receive obJect required on this line

result=xmlresponse.selectSingleNode('//y:hintResult').text;


I ve been working on this as well but seem to have issues with it, I think they are related to my Namespaces variable..
Code:
 function placeHint(xmlresponse) {
 var resultNode;
 oDoc =new ActiveXObject("MSXML2.DOMDocument.4.0")  
 oDoc.async = false;
 oDoc.load(xmlresponse)
 var sNamespaces = "xmlns:soap='[URL unfurl="true"]http://www.w3.org/2001/XMLSchema'[/URL] "+ "xmlns:default='[URL unfurl="true"]http://microsoft.com/webservices/'";[/URL]
 oDoc.setProperty("SelectionNamespaces",sNamespaces);
 var sXPath="/*/soap:Body/default:hintResponse/default:hintResult";
 resultNode = oDoc.selectSingleNode(sXPath);
 alert(resultNode.text);
}
any thoughts?


MCP, .Net Solutions Development <%_%>
 
ok, i didnt realize http posting was that much easier :)
Ive switched a bit to http post through code

Here's the response i get searching for e
Code:
<?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="[URL unfurl="true"]http://microsoft.com/webservices/">Brittany</string>[/URL]

Here's my code... again
Code:
function postdata(){
var url="[URL unfurl="true"]http://localhost:3601/WebSite2/hint.asmx/hint";[/URL]
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open('POST', url , true);
xmlHttp.setRequestHeader("Content-Type","application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
xmlHttp.send("Term=e");
xmlHttp.onreadystatechange = checkState;
 }
function checkState(){
  if(xmlHttp.readyState=="complete"||xmlHttp.readyState==4){
   placehint(xmlHttp.responseXML);
      }
  else{
  alert("Not Complete");
  }
} 
 function placeHint(xmlresponse) {
 oDoc =new ActiveXObject("MSXML2.DOMDocument.4.0")  
 oDoc.async = false;
 var sNamespaces = "ph:xmlns='[URL unfurl="true"]http://microsoft.com/webservices/'";[/URL]
 oDoc.setProperty("SelectionNamespaces",sNamespaces);
 oDoc.load(xmlresponse)
 var resultNode = oDoc.selectSingleNode("/*/ph:string");

[COLOR=red]BUT I STILL RECEIVE OBJECT REQUIRED RIGHT HERE! IS THERE A PROBLEM WITH MY DECLARATION OF resultNode?[/color] 
alert(resultNode.text);
}

MCP, .Net Solutions Development <%_%>
 
well i decided to go with a nice easy http post method and an xslt on the response rather than traversing xml in
javascript and fighting all those namespaces :)

it works fabulously and is really gonna add a dynamic feel across the board on my app

<script language=javascript type="text/javascript">
//posting data to webservice
function postdata(){
var url="xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
xmlHttp.open('POST', url , true);
xmlHttp.setRequestHeader("Content-Type","application/x-var sendText=document.getElementById("Term").value
xmlHttp.send("Term="+sendText);
xmlHttp.onreadystatechange = checkState;
}
//checking for readiness
function checkState(){
if(xmlHttp.readyState=="complete"||xmlHttp.readyState==4){
placeHint2(xmlHttp.responseXML);
}
}
//adding data to page through transform
function placeHint2(xmlresponse){
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load(xmlresponse)
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("hint.xsl")
// Transform
document.getElementById("suggested").innerHTML = xml.transformNode(xsl);
}
</script>


MCP, .Net Solutions Development <%_%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top