Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript
Megnut

Web Topics
All Articles
Browsers
ColdFusion
CSS
Database
Flash
Graphics
HTML/XHTML/DHTML
Scripting Languages
Tools
Weblogs

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab






Using the Mozilla SOAP API
Pages: 1, 2, 3, 4, 5

Creating the SOAPCall

Once the script has been given permission to proceed, the next order of business is to create a SOAPCall object:



function callGoogle(method,params,callback){
  try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  } catch (e) {
      alert(e);
    return false;
  }
  var soapCall = new SOAPCall();
  soapCall.transportURI = "http://api.google.com/search/beta2";
}

The transportURI property of the SOAPCall object is a string indicating the endpoint URI of the service. You can find this near the end of the Google WSDL file, under the SERVICE element.

Next, the message must be encoded with the requested method and provided parameters. This is done by passing the following arguments to the encode method of the SOAPCall object:

soapCall.encode(0, method, "urn:GoogleSearch", 0, null, params.length, params);

The above is a bit tricky to describe because some of the arguments aren't necessary for basic SOAP operations, and therefore have zero and null values. The only arguments you need be concerned with are method, which is the service method to be invoked, and the target namespace of the service (in this case, the namespace "urn: GoogleSearch", which can be found in the WSDL file as the value of the targetNamespace attribute, under the DEFINITIONS element).

The final two arguments supply the encode method with the number of parameters to be passed to the service method and the array of parameters, respectively.

Invoking the Call

Now you're ready to invoke the SOAPCall and send the service request to Google. The asyncInvoke method of the SOAPCall object takes one argument: a pointer to a callback function to handle the SOAP response.

As noted above, the callGoogle function requires such a callback as its third argument, but I'm not ready to use that just yet. Instead, I'm going to do a little error-checking before handing the response off to the rest of the application. So I'll pass it to handleSOAPResponse:

function callGoogle(method,params,callback)
{
  try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  } catch (e) {
      alert(e);
    return false;
  }
  var soapCall = new SOAPCall();
  soapCall.transportURI = "http://api.google.com/search/beta2";
  soapCall.encode(0, method, "urn:GoogleSearch", 0, null, params.length, params);
  soapCall.asyncInvoke(
    function (response, soapcall, error)
     {
        var r = handleSOAPResponse(response,soapcall,error);
        callback(r);
      }
  );
}

The last part of this example requires a little more explanation. When the callback passed to asyncInvoke executes, it's provided with three arguments which come as a result of the SOAP call being completed:

  • response: a SOAPResponse object containing the returned data from the service.
  • soapcall: a pointer to the original SOAPCall object responsible for this message. This is useful if you have several SOAPCall objects in use and need to differentiate between them inside a single callback.
  • error: a status code. A value other than zero indicates an error.

Since you want the response to go to handleSOAPResponse for error-checking, and then to the callback initially supplied to callGoogle, you should wrap the two handlers in an anonymous function to be passed to the asyncInvoke method. Remember that asyncInvoke does not wait for a response from the service before allowing the script to continue. Doing it this way ensures that the script doesn't forget the original callback when the callGoogle function ends.

OK, you've completed the callGoogle function for assembling and sending SOAP messages to Google. Now it's time to use handleSOAPResponse to deal with the response.

Handling the SOAP Response

The handleSOAPResponse function receives the results of the SOAP call and checks to make sure there wasn't a SOAP fault or service error. If no faults or errors are found, the script assumes the response is safe and passes the SOAPResponse to the user-defined callback originally given to callGoogle.

Checking for Errors

The first step is to see if the error argument contains a value other than zero. If so, we can assume a fatal error has occurred and exit the script:

function handleSOAPResponse (response,call,error)
{
    if (error != 0)
    { 
        alert("Service failure");
        return false;
    } 
}

If no error is found here, the script proceeds to check for a SOAP fault.

Pages: 1, 2, 3, 4, 5

Next Pagearrow