Invoking the SOAP Web service with Ajax: Building a Web Service client (2)

Source: Internet
Author: User
Tags array object extend header object model string web services wrapper
Ajax|web|web Service | client WS. The Call object exposes the following three methods:

Add_handler. Add a request/response handler to the processing chain. The handler object is invoked before and after calling the Web service to support extensible pre-call processing and post-call processing.

invoke. Will specify the SOAP. The Envelope object is sent to the Web service, and then the callback function is invoked after the response is received. Use this method when calling a document-style WEB service that uses text XML encoding.

Invoke_rpc. Creates a SOAP that encapsulates the RPC style request. Envelope and sends it to the WEB service. When a response is received, the callback function is invoked.

In the usual case, WS. The call object is simply a thin wrapper (thin wrapper) at the top of the XMLHttpRequest object that can perform a number of simplified processing operations. These actions include setting the SOAPAction HTTP Header required by the SOAP 1.1 specification.

Using Ws.js


The WEB Services JavaScript Library provides a very simple API.

soap.* objects (Soap.element, SOAP. Envelope, SOAP. Header and SOAP. Body) provides a way to build and read a SOAP envelope, as shown in Listing 3, so that the underlying details of the processing XML Document Object model are smoothly abstracted.

Listing 3. Building a SOAP Envelope




				

var envelope = new SOAP. Envelope (); var body = Envelope.create_body (); var el = body.create_child (new WS. QName (' method ', ' Urn:foo ')); El.create_child (New WS. QName (' param ', ' urn:foo ')).





Listing 4 shows the SOAP envelope generated by the code in Listing 3.

Listing 4. Building a SOAP Envelope


				

Bar





If the SOAP envelope you are creating represents an RPC-style request, then soap. The BODY element provides an easy way to SET_RPC (as shown in Listing 5), which constructs a complete RPC request--containing a specified operation name, a specified array of input parameters, and a URI for a SOAP-encoded style.

Listing 5. Building an RPC request envelope


var envelope = new SOAP.Envelope();

var body = envelope.create_body();

var el = body.create_child(new WS.QName('method','urn:foo'));

el.create_child(new WS.QName('param','urn:foo')).
set_value('bar'); 



Each parameter is passed as a JavaScript object structure and may have the following properties:

name. A string or WS that specifies the name of the parameter. QName object. Necessary.

value. The value of the parameter. If the value is not a simple data type (for example, a string, integer, or other), you should specify a WS that serializes the value into the appropriate XML structure. Binder. Necessary.

Xsitype: WS of the XML schema instance type that identifies the parameter. QName (for example, xsi:type= "int" corresponds to xsitype:new WS.) QName (' int ', ' Http://www.w3.org/2000/10/XMLSchema ')). Optional.

Encodingstyle: The URI that identifies the SOAP encoding style used by the parameter. Optional.

Binder: A WS that can serialize arguments to XML. Binder implementation. Optional.

For example, if you want to specify a parameter named "ABC", XML namespace is "Urn:foo", Xsi:type is "int" and the value is "3", I use the following code: The new Array ({name:new WS). QName (' abc ', ' Urn:foo '), Value:3, Xsitype:new WS. QName (' int ', ' Http://www.w3.org/2000/10/XMLSchema ')}).

Once I have constructed SOAP for a service request. Envelope, I'll take that SOAP. Envelope passed to WS. The Invoke method of the Call object in order to invoke the method encoded in the envelope: (New WS. Call (Service_uri)). Invoke (envelope, callback) Another option is to manually build SOAP. Envelope. I'll take the argument WS. QName, parameter arrays, and encoding styles are passed to WS. The Invoke_rpc method of the Call object, as shown in Listing 6.

Listing 6. Use WS. The call object invokes the Web service


				

var Nsuri = ' Urn:foo '; var qn_op = new WS. QName (' method ', Nsuri); CALL.INVOKE_RPC ( Qn_op, New Array ( {name: ' param ', Value: ' Bar '} ), SOAP. Noencoding, function (Call,envelope) { Envelope is the response SOAP. Envelope The XML Text of the response is in arguments[2] } );





When invoking the Invoke method or the Invoke_rpc method, WS. The call object creates a basic XMLHttpRequest object, passes the XML element containing the SOAP envelope, receives and resolves the response, and then invokes the provided callback function.

In order to be able to extend the preprocessing and reprocessing of SOAP messages, WS. The Call object allows you to register a set of WS. The Handler object, as shown in Listing 7. These objects are called for each request, every response, and every error in the call cycle. Can be extended by the WS. Handler a JavaScript object to implement a new handler.

Listing 7. Create and register a response/response handler


				

var MyHandler = Class.create (); Myhandler.prototype = (new WS. Handler ()). Extend ({ On_request:function (envelope) { Pre-request processing }, On_response:function (Call,envelope) { Post-response, Pre-callback processing }, On_error:function (Call,envelope) { } });

var call = new WS. Call (...);





Handlers are most useful for inserting or extracting information in a SOAP envelope that is being passed. For example, you can imagine that a handler automatically inserts the appropriate Web service addressing (Web Services addressing) element into the Header of the SOAP Envelope, as shown in the example in Listing 8.

Listing 8. A handler example that adds a WEB service addressing operation Header to the request


				

var wsaddressinghandler = Class.create (); Wsaddressinghandler.prototype = (new WS. Handler ()). Extend ({ Envelope.create_header (). Create_child ( New WS. QName (' Action ', ' http://ws-addressing ', ' WSA ') ). Set_value (' http://www.example.com '); }





Ws. The Binder object (listing 9) executes SOAP. The custom serialization and deserialization of the Element object. Ws. The implementation of Binder must provide the following two methods:

to_soap_element. Serializes a JavaScript object into SOAP. Element. The first parameter passed in is the value to serialize. The second parameter is SOAP. Element, you must serialize the values that you want to serialize to SOAP. Element. The method does not return any values.

To_value_object. The SOAP. The Element is deserialized to a JavaScript object. The method must return a deserialized value object.

Listing 9. Ws. Binding Implementation Example





				

var mybinding = Class.create (); Mybinding.prototype = (new WS. Binding ()). Extend ({ ... }, To_value_object:function (Element) { ... }






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.