How to properly use XML in Ajax development (1)

Source: Internet
Author: User
Tags form post

Now, no meaningful programming can be performed without using XML. Whether you are looking at Web designers turning to XHTML, Web programmers using JavaScript, server programmers using deployment description files and data binding, or back-end developers studying XML-based databases, all are using this extensible markup language. Therefore, XML is considered to be one of the core technologies at the bottom of Ajax.

However, this view reflects that Ajax applications are represented by the name selected by their core object-XMLHttpRequest. This name is not very good because it does not reflect the actual technical situation. In other words, most people think that XML is the core component of Ajax, just because they assume that XMLHttpRequest objects use XML at any time. But this is not the case. The first part of this article provides the reason. In fact, XML rarely appears in most Ajax applications.

XML is indeed applied in Ajax, and XMLHttpRequest also supports this usage. There is nothing that can block you from sending XML to the server. In the previous articles in this series, we use common text and name/value parameters to send data, but XML is also a feasible format. This article describes how to do this. But most importantly, I will discuss why XML can be used as the request format and why it should not be used in most cases.

XML: Is it useless?

Ajax applications and their use of XML can easily be taken for granted: The name of this technology Ajax) and the core object XMLHttpRequest used) all imply the use of XML, XML is also frequently heard when talking about Ajax applications. However, this is a big mistake. If you want to be confident in writing asynchronous applications, you must know that this idea is wrong and you 'd better know why it is wrong.

XMLHttpRequest: Bad Name and HTTP

One of the worst possible situations for a technology is that it becomes so popular that it cannot change some of its basic content. XMLHttpRequest is the basic object used in Ajax applications. It sounds like it was designed to send XML requests through HTTP requests or send HTTP requests in some XML format. However, no matter what the object name sounds like, what it actually does is to provide an HTTP Request Method for client code, which is usually JavaScript on the webpage. That's all. Nothing else.

Therefore, it may be better to change XMLHttpRequest to a more accurate name, such as HttpRequest or simple Request. However, tens of thousands of people now use Ajax in their applications, and we know it will take several years, if not a dozen years) most users use new browsers such as Internet Explorer 7.0 or Firefox 1.5. Therefore, such modifications are not feasible. In the end, we have to use XMLHttpRequest, which requires developers to know the fact that its name is not real.

To some extent, one of the best backtracing methods for browsers that do not support XMLHttpRequest, especially on Windows) is to use Microsoft IFRAME objects. It does not sound like XML, HTTP, or request, does it? Of course, all these may be involved, but this clearly shows that the XMLHttpRequest object is more about sending requests without reloading the page, rather than involving XML or even HTTP too much.

The request is HTTP rather than XML.

Another common mistake is to think that XML is used behind the scenes-Frankly speaking, I think so too! However, your opinion shows that you are not familiar with this technology. When a user opens a browser to request a webpage from the server, something like a http://www.google.com or a http://www.headfirstlabs.com is entered. Even if you do not enter http: //, the browser will add this part in the address bar. The first part, namely http: //, is a very intuitive clue about how to communicate: HTTP through Hypertext Transfer protocol. When writing code on a webpage to communicate with the server, both Ajax, normal form POST, and even hyperlinks are used to deal with HTTP.

Since all Web communication between the browser and the server is carried out through HTTP, the idea that XML is a transmission technology used behind XMLHttpRequest is unreasonable. Of course, XML can be sent in HTTP requests, but HTTP is a precisely defined protocol and cannot disappear in a short time. In addition to explicitly using XML in requests or sending responses using XML, the XMLHttpRequest object only uses common HTTP. Therefore, when someone says "Oh, XMLHttpRequest is used behind the scenes", you 'd better laugh and patiently explain what HTTP is, it tells them that although XML can be sent over HTTP, XML is a data format rather than a transmission protocol. Through this discussion, we can deepen our understanding of it.

Use XML)

So far, I am talking about where Ajax does not use XML. However, XML in x and XMLHttpRequest in Ajax still has its practical significance. There are multiple options for using XML in Web applications. This section will discuss the basic options, and the remaining sections will discuss the details in depth.

XML options

There are two basic usage of XML in asynchronous applications:

  • Send requests from webpages to servers in XML format
  • Receive requests from the server in XML format on the webpage

The first method is to send a request in XML. You need to set the request format to XML. You can use the API to complete the request, connect the request to the text, and then send the result to the server. In this way, the main task is to construct a request in a way that complies with XML rules and can be understood by the server. Therefore, the key here is actually the XML format. After obtaining the data to be sent, you only need to wrap it in XML syntax. This article will discuss the usage of XML in Ajax Applications later.

The second method is to use XML to receive requests. You need to receive responses from the server and then extract data from the XML. You can use APIs or the brute force method ). In this case, the key lies in the data from the server, and you just need to extract the data from the XML for use. This is the topic of the next article in this series. We will discuss it in detail later.

Some advice

Before discussing the use of XML in detail, I would like to give you a piece of advice: XML is not a concise, fast, and space-saving format. In the following sections and the next article in this series, we will see that using XML in the context does have some good reasons, especially the response and request of XML and common text) there are some advantages in comparison. However, compared with common text, XML usually occupies more space and is slower, because the labels and semantics required by XML must be added to the message.

If you want to write a program that is fast and looks like a desktop application, XML may not be the best choice. If you start with plain text and find that XML is really needed, you can use it. However, if you use XML from the very beginning, it will certainly reduce the responsiveness of the application. In most cases, Sending plain text is faster than converting text into the following XML, using name/value pairs like name = jennifer ):

     
      jennifer
     

See where the XML processing time is added: Wrap the text into XML; when sending additional information, note that I do not contain any surrounding elements, XML headers, or other content that may appear in the actual request.) Let the server Parse XML, generate a response, and wrap the response in XML, and send it back to the web page; let the web page parse the response, and finally use it. Therefore, you need to know when to use XML. Do not initially think that it can accelerate the application in many cases; but it can enhance flexibility, which we will discuss now.

XML from client to server

Let's take a look at XML as the format for sending data from the client to the server. We will first discuss the technical implementation, and then spend some time analyzing when it is appropriate and not suitable for use.

Sending name/value pair

In the 90% Web application you wrote, the name/value pairs will eventually be sent to the server. For example, if you enter the name and address in the webpage form, you may want the data to take the following form:

firstName=LarrylastName=Gullahornstreet=9018 Heatherhorn Drivecity=Rowlettstate=TexaszipCode=75080

If you use plain text to send the data to the server, you can use the code shown in Listing 1. Similar to the example used in the first article in this series.

Listing 1. Using plain text to send name/value pairs

function callServer() {  // Get the city and state from the Web form  var firstName = document.getElementById("firstName").value;  var lastName = document.getElementById("lastName").value;  var street = document.getElementById("street").value;  var city = document.getElementById("city").value;  var state = document.getElementById("state").value;  var zipCode = document.getElementById("zipCode").value;  // Build the URL to connect to  var url = "/scripts/saveAddress.php?firstName=" + escape(firstName) +    "&lastName=" + escape(lastName) + "&street=" + escape(street) +    "&city=" + escape(city) + "&state=" + escape(state) +    "&zipCode=" + escape(zipCode);  // Open a connection to the server  xmlHttp.open("GET", url, true);  // Set up a function for the server to run when it's done  xmlHttp.onreadystatechange = confirmUpdate;  // Send the request  xmlHttp.send(null);}


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.