Online Book Reader

Home Category

AJAX In Action [129]

By Root 3925 0
phrase we’re submitting and the license key value. SOAP is designed with automation in mind, and it is unusual to build the XML by hand as we have done here. Both Internet Explorer and Mozilla provide browser-specific objects for interacting with SOAP in a simpler fashion. Nonetheless, we thought it instructive to do it manually and look at the SOAP request and response data.

Having created the request XML text, we construct a ContentLoader object d, passing in the SOAP XML as the HTTP body content, along with the URL of the Google API e and the custom HTTP headers f. We set the content-type to text/

xml. Note that this represents the MIME type of the body of the request, not the MIME type we expect to receive in the response, although in this case the two are the same. The final parameter, set to a value of true, indicates that we should seek permission from the PrivilegeManager object.

Parsing the response

The ContentLoader will then make the request and, if the user grants permission, will receive an equally large chunk of XML in return. Here is a small sample of the response to a search on the term “Ajax”:

xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/xmlschema-instance"

xmlns:xsd="http://www.w3.org/1999/xmlschema">

xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/"

xsi:type="ns2:array"

ns2:arraytype="ns1:directorycategory[1]">

...

false

xsi:type="xsd:int">741000

...

Licensed to jonathan zheng

Communicating with remote services

261

truerelatedinformationpresent>

de officiële site van afc <b>ajax</

b>.

official club site, including roster, history, wallpapers, and video clips.<br> [english/dutch]

<p><b>ajax</b>.nl – splashpagina<p>

...

The full SOAP response is too lengthy to include here, we’ve presented three snippets. The first part defines some of the transport headers, saying where the response comes from, and so on. Within the body, we find a couple of elements describing the estimated results count—the phrase returned 741,000 results, which is not considered to be an exact figure. Finally, we can see part of the first result returned, describing the link to the Dutch football team Ajax’s home page. Listing 7.4 shows our callback handler, in which we parse the response. Listing 7.4 parseGoogleResponse() function

function parseGoogleResponse(){

var doc=this.req.responseText.toLowerCase();

document.getElementById('details').value=doc;

var startTag=''; var endTag='';

var spot1=doc.indexOf(startTag);

var spot2=doc.indexOf(endTag);

var strTotal1=doc.substring(spot1+startTag.length,spot2);

var total1=parseInt(strTotal1);

var strOut="";

if(total1>=intNum && total1<=intNum+guessRange){

strOut+="You guessed right!";

}else{

strOut+="WRONG! Try again!";

}

strOut+="
Your search for "

+document.Form1.yourGuess.value

+" returned " + strTotal1 + " results!"; document.getElementById("spanResults").innerHTML = strOut;

}

For the moment, we aren’t concerned with the structure of the SOAP message but only with the estimated number of results returned. The response is valid XML, Licensed to jonathan zheng

262

CHAPTER 7

Security and Ajax

and we could parse it using the XMLHttpRequest object’s responseXML property. However, we take the path of least resistance here, and simply extract

Return Main Page Previous Page Next Page

®Online Book Reader