XForms/Web service

Web services can be called directly from an XForms application using the  element in the model.

Connecting a web service to an Event
To call a web service you first need to "wire it" up to an event. For example this can be a "submit" button at the end of a form.

The submit element is usually in the presentation (inside the body tag) and is wired to a element in the model.

This is just a temporary storage area that the results will be inserted into.

The XForms Statement
There are few things to note. The  attribute is the URL of the connection point of the web service. The operation you call is not included in the URL.

The method (get, put or post) in this case is a post.

Mediatype
XForms is a rich application development standard. So by default the HTTP transactions used by XForms use a mediatype for XML application that includes binary files and binary attachments. So by default, XForms sends an HTTP command with the following in it:

However most simple web services do not support binary. The  for ASCII text (without binary) is just:

So if you will also be sending binary files you will need to add a mediatype attribute to to your submission:

Debugging Response Documents
You can also replace the entire document with the XML result (the option is replace="all") or you can just have it replace an instance.

The last three statements are for displaying results.

A style sheet
This makes the error messages red and the correct responses green.  @namespace xf url("http://www.w3.org/2002/xforms"); body {font-family: Helvetica, sans-serif} .code {font-family: Courier New, fixed-width; font-weight:bold;} .error-message {font-weight:bold; color: red} .ok {font-weight:bold; color: green} xf|output {font-weight:bold; font-size: 16pt}

The Presentation
Here is the actual body of the XForms document. It has a single button that calls the web service. Web Service Demo Note: With some browsers (FireFox) you must add the appropriate hosts to your XForms "white list" for this application to run. With FireFox see Tools/Options/Content tab.  Call Web Service  Current form status:  Status: Ready to call web service. Waiting for response...please stand by...  Submission error. Did you remember to allow XForms to submit data to other domains?   Results returned successfully   <xf:label>Return value: </xf:label> </xf:output> </xf:group> </xf:case> </xf:switch>

Discussion
The presentation conditionally displays the error messages using a switch/case section. The toggle elements are each triggered by the appropriate event.

You can also put a spinning GIF icon in the section to get an animation for a response from the server.