AJAX In Action [129]
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:xsd="http://www.w3.org/1999/xmlschema"> xsi:type="ns2:array" ns2:arraytype="ns1:directorycategory[1]"> ... ... Licensed to jonathan zheng Communicating with remote services 261 relatedinformationpresent> b>. <b>ajax</b>.nl – splashpagina ... 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 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+=" +document.Form1.yourGuess.value +" } 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
Your search for "