Ajax and Scripting Web Services using E4X (2)

Source: Internet
Author: User
Tags object error handling http request include net string version web services
Ajax|web|web Services | scripts

For each (var p in X.person) {

if (p.height. @measure = = "Metric")

P.height=metrictoimperial (P.height);

}

print (x);

Here is the output:

<people>

<person gender= "Male" >

<name>Ant</name>


<eyes>Blue</eyes>


<feet>5</feet>

<inches>11</inches>


</person>

<person gender= "Male" >

<name>Paul</name>


<eyes>Grey</eyes>


<feet>5</feet>

<inches>12</inches>


</person>

</people>

XML Namespaces in e4x (Namespace)

If you're an XML whiz, you might want to know how to use this syntax to manage XML namespaces at this point. Here are three ways to implement this:

First, you can use the embedded XML syntax:

var soapmsg = <s:envelope xmlns:s= "Http://www.w3.org/2003/05/soap-envelope"/>;

Print (Soapmsg.namespace ());

Http://www.w3.org/2003/05/soap-envelope

The second method is to set the default XML namespace before creating the element:

Default XML namespace = new namespace ("Http://www.w3.org/2003/05/soap-envelope");

You can set it to an empty string to reset the default namespace, for example:

Default XML namespace = ""

The last method is to use:: operator

var f = new Namespace ("http://fremantle.org");

Soapmsg.body.f::getstockquote= "IBM";

Print (soapmsg);

<s:envelope xmlns:s= "Http://www.w3.org/2003/05/soap-envelope" >

<s:Body>

<frem:getstockquote xmlns:frem= "http://fremantle.org" >

Ibm

</frem:GetStockQuote>

</s:Body>

</s:Envelope>

XML element Sorting

An obvious advantage of e4x is that it fully supports XML, including sorting capabilities. Many direct mappings from XML documents to corresponding programming language objects only support XML semantic subsets that conform to common object semantics-thus losing the ability to represent objects as well as documents. However, for this article, it is not appropriate, as long as a simple review of the specification can be seen, e4x XML objects have the built-in capabilities of the XML elements can be meticulous and accurate sorting.

Using Javascript expressions in XML

Before we turn to the WEB service, we'll introduce you to the last one--using curly braces "{}". The above is a description of "embedded" XML. E4X also allows you to re-enter the JAVASCRIPT environment and can include the evaluated expression values. For example:

var num = 36;

var p = <paul><age>{num}</age></paul>

Print (p);

<paul>

<age>36</age>

</paul>

So far, we've covered the basics of e4x, so we can use these basics to do the following.

Using E4X to invoke a WEB service

In this section, we will describe how to use e4x in the following two environments:

1. Mozilla 1.8 using XMLHttpRequest

2. Java/rhino

You can easily invoke a Web service using e4x in your browser. But there's a problem! So far, the only browser that supports E4X is the Pro version of Mozilla 1.8. However, we do not recommend it as a portable cross-browser solution, and the following example demonstrates how E4X can invoke a Web service in an easy way. In the next section, we'll look at another way to run in the Rhino JavaScript engine.

Ajax

The sample instance shows the browser that sends and receives SOAP messages to the SOAP server. To do this, we use E4X by XMLHttpRequest objects. This object is very useful, and Mozilla and Internet Explorer browsers support it, allowing the scripting language running inside the browser to generate HTTP requests in the background. In fact, that's why Google's GMail can do anything. This architecture has recently been killed as asynchronous Javascript+xml (AJAX).

Essentially, AJAX is designed to improve the responsiveness and usability of WEB pages by interacting with servers in a more flexible way than the standard HTML and HTTP "page" models. A good example of this is the Google Maps beta, which is significantly more interactive than the previous mapping Web site.

Encouragingly, according to news reports, the performance of the AJAX combined with e4x is better! We will show you two versions of the browser application. The first version demonstrates the interaction, while the second version hides the buttons and internal work processes on the Web page to demonstrate interactivity and asynchrony.

Security for browsers

To demonstrate the security of the browser, we used the standard WEB services available on the xmethods.net. However, there is a problem, because in general, the browser's security rules do not allow scripting or Java applets to create a network connection, unless you are establishing a network connection to the server that generated the page. Otherwise, you need to have a monitoring page that will "copy" your key action to another server.

But you can avoid this kind of operation. To do this, you need to take two steps. First, you must enable the upgrade privileges in the Mozilla configuration for scripting programs. (assuming you have downloaded and installed the Mozilla 1.8 beta).

Enter About:config in the address bar of the browser, and then update the Signed.applets.codebase_principal_support value from False to true. (For security reasons, remember to set the original value back after you complete the operation.) )

Then, in a script program, you can request the use of upgrade privileges. When the scripting language runs, the user is prompted to use these privileges. Code behavior:

Netscape.security.PrivilegeManager.enablePrivilege ("Universalxpconnect universalbrowseraccess");

The other option is to deploy services and Web pages to Web application servers such as Ibm®websphere®application server or Tomcat. This approach, for example, applies to the example of a default stock quote machine with Apache axis and axis.
Stock Quote Client Sample


This script is part of the stockclient.html. If you download the Ws-ajax1code.zip file from this article, unzip the zip content and then use Mozilla to open stockclient.html, you will see the following:

Figure 1. The stockclient.html in Mozilla

To validate it, first click the Update URL. This action is done by using the XMLHttpRequest object from HTTP://SERVICES.XMETHODS.NET/SOAP/URN:XMETHODS-DELAYED-QUOTES.WSDL (or any URL you type in the WSDL box) Get the WSDL file, and then use E4X to get the port address URL from there. Now click Send, and you will see that the SOAP request is filled in. After two seconds, this SOAP response should be updated with the result field. Let's look at the code.

The Stock Quote Client script program

The script invokes the specified URL with request for the IBM stock price. If you are using the Axis server, then we recommend using the ticker symbol XXX, which is a special symbol--the deployed service will always return a fixed response for the tape recorder, rather than generating a WEB request to get the real stock price--so it would be better to test it with that symbol.

The first thing you have to do is define the e4x you want to use:

<script type= "Text/javascript;e4x=1" >

When you press the Send button, the script appears as follows:

var s = new Namespace (

"S",

"http://schemas.xmlsoap.org/soap/envelope/");

var envelope = <s:envelope xmlns:s={s}/>;

Envelope.s::body= "";

var body = Envelope.s::body;

This operation is generic to any SOAP request . it simply creates a SOAP envelope and does not include any message bodies . An equivalent implementation of this operation is shown below:

var envelope =

<s:envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/" >

<s:Body/>

</s:Envelope>

However, the preceding code is easier and gives you pointers to the principal element .

The next step is to create the body of the message:

var x = new Namespace ("X", "urn:xmltoday-delayed-quotes");

Body.x::getquote = <x:getquote xmlns:x={x}/>;

Finally, you must add the correct symbols:

var symbol = document.getElementById ("symbol"). Value;

var getquote = Body.x::getquote;

Getquote.symbol=symbol;

Now you have a fully formed SOAP request message . If you want to test the message envelope, you need to follow these steps:

<s:envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/" >

<s:Body>

<x:getquote xmlns:x= "Urn:xmethods-delayed-quotes" >

<symbol>XXX</symbol>

</x:getQuote>

</s:Body>

</s:Envelope>

In order to send it, you must use the XMLHttpRequest object . we have created a simple helper function to support the use of XMLHttpRequest objects to invoke services that use e4x . This execservice function supports not only asynchronous methods but also synchronous methods .

function Execservice (URL, XML, callback) {

var xmlhttp = new XMLHttpRequest ();

var Async=false;

if (arguments.length==3) async=true;

Xmlhttp.open ("POST", url, async);

Xmlhttp.setrequestheader ("SOAPAction", "\" \ ")"

Xmlhttp.setrequestheader ("Content-type", "Text/xml")

if (async) {

var f = function () {

if (xmlhttp.readystate==4) {

Callback (New XML (Xmlhttp.responsetext));

}

}

Xmlhttp.onreadystatechange = f;

}

Xmlhttp.send (Xml.tostring ());

if (!async) return new XML (Xmlhttp.responsetext);

}

Let's take a look at the code in detail here . first, the code supports two ways of calling . you can use one of these:

XML execservice (string URL, XML envelope), or void Execservice (string URL, XML envelope, function callback);

In this case, the callback function should be a void callback (XML x).

You can then use this function to invoke the XML service directly and wait for the response, or you can transfer a function that calls the function using an XML response message .

The function determines whether it is asynchronous or synchronous (3 is asynchronous) based on the number of parameters, and then simply uses the XMLHttpRequest object to post the XML message to the HTTP request information and send the request to the specified URL.

    • Ajax: A new way to build Web apps
    • Discussion on the error handling mechanism of AJAX (2)
    • Discussion on the error handling mechanism of AJAX (1)
    • First experience. NET Ajax Brushless New technology
    • A brief analysis of Ajax development Technology in Rails system (4)


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.