Online Book Reader

Home Category

AJAX In Action [128]

By Root 3898 0
then, to send a request off to Google. Listing 7.2 shows the HTML required for our simple guessing game application.

Listing 7.2 googleSoap.html

Licensed to jonathan zheng

258

CHAPTER 7

Security and Ajax

Google Guessing

Obtain a search that returns between 

  results!




We set up the form elements in HTML, and calculate a suitably large random number here. We also declare a variable, googleKey. This is a license key allowing us to use the Google SOAP APIs. We haven’t included a valid key here, because we aren’t allowed to by the license terms. Keys are free, and offer a limited number of searches per day. They can be obtained from Google online through a simple process (see the URL in the Resources section at the end of this chapter). Submitting the request

The bulk of the work is done by the submitGuess() function, which is invoked when the form is submitted. This is defined in the included JavaScript file, so let’s have a look at that next. Listing 7.3 illustrates the first bit of JavaScript, which calls the Google API.

Licensed to jonathan zheng

Communicating with remote services

259

Listing 7.3 submitGuess() function

function submitGuess(){

if (!googleKey){ b

Check license key

alert("You will need to get a license key "

+"from Google,\n and insert it into "

+"the script tag in the html file\n "

+"before this example will run.");

return null;

}

var myGuess=document.Form1.yourGuess.value;

var strSoap='' c Build SOAP message

+'\n\n+' 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:ns1="urn:GoogleSearch"'

+' SOAP-ENV:encodingStyle='

+'"http://schemas.xmlsoap.org/soap/encoding/">'

+'' + googleKey + ''

+''+myGuess+''

+'0'

+'1'

+'true'

+''

+'false'

+''

+'latin1'

+'latin1'

+''

+''

+'';

var loader=new net.ContentLoader( d

Create ContentLoader

"http://api.google.com/search/beta2", e Provide URL to Google API parseGoogleResponse,

googleErrorHandler,

"POST",

strSoap,

"text/xml",

{

Man:"POST http://api.google.com/search/beta2 HTTP/1.1", MessageType:"CALL"

Pass custom f

},

HTTP headers

true

);

}

Licensed to jonathan zheng

260

CHAPTER 7

Security and Ajax

The first thing that we do in the submitGuess() function is check that we have a license key, and remind the user if we don’t b. When you download the code for this example, the license key will be set to null, so you’ll need to get your own key from Google if you want to play with it.

Our second task is to construct a monstrously huge SOAP message c, containing the

Return Main Page Previous Page Next Page

®Online Book Reader