Mastering Ajax, part 7th: Using XML in requests and responses

Source: Internet
Author: User
Tags form post http request

Understand when it is appropriate to use XML

Introduction: Developers who occasionally use Ajax will also notice X in Ajax and realize that it represents XML. XML is one of the most commonly used data formats in programming, and can provide tangible benefits for server responses in asynchronous applications. In this article, you will see how the server sends XML in the request response.

No meaningful programming can now be done without using XML. This extensible Markup language is used regardless of whether you consider turning to XHTML web designers, web programmers who use JavaScript, server-side programmers using deployment profiles and data binding, or back-end developers who study xml-based databases. It is therefore not surprising that XML is considered one of the core technologies at the bottom of Ajax.

However, this view is reflected in the fact that the Ajax application is represented in the name--xmlhttprequest of its core object, which is not very good because it does not reflect the technical reality. In other words, most people think that XML is a central part of Ajax simply because they assume that XMLHttpRequest objects use XML at all times. But this is not the case, the first part of this article gives the reason. In fact, you'll see that XML rarely appears in most Ajax applications.

XML does have applications in Ajax, and XMLHttpRequest also supports this usage. There's really nothing that can stop you from sending XML to the server. In the previous articles in this series, we used plain text and name/value parameters to send data, but XML is also a viable format. This article will describe how to do this. But most importantly, I'll discuss why XML can be used as a request format and why it should not be used in most cases.

XML: Is it useless?

It's easy to take for granted the AJAX applications and their use of XML: The name of the technology (AJAX) and the core objects it uses (XMLHttpRequest) imply the use of XML, which is often heard when it comes to Ajax applications. However, this view is a big mistake, and if you want to be truly confident in writing an asynchronous application, you must know that the idea is wrong, and it is best to know why it is wrong.

XMLHttpRequest: Bad Name and HTTP

One of the worst things a technology might encounter is that it becomes too hot to change some of its basics. XMLHttpRequest is precisely the case, which is the basic object used in Ajax applications. It sounds like it was designed to send XML over HTTP requests or to send an HTTP request in some kind of XML format. But whatever the name of the object sounds like, what it does is simply provide a way for the client code (usually JavaScript in the Web page) to send an HTTP request. No, nothing more.

Therefore, it may be better to change the XMLHttpRequest to a more accurate name, such as HttpRequest, or simply Request. But now tens of thousands of people have used Ajax in their applications, and we know that it will take years (if not more than 10 years) for most users to switch to Internet Explorer 7.0 or Firefox 1.5, the new versions of browsers, So the modification is actually not feasible. Ultimately, we have to use XMLHttpRequest, which requires the developer to know the fact that its name is inconsistent.

To some extent, one of the best backtracking methods for browsers that do not support XMLHttpRequest, especially on Windows, is to use Microsoft IFRAME objects. It doesn't sound like XML, HTTP, or request, does it? Of course, all of this may be involved, but it's clear that the--xmlhttprequest object is more about making requests without reloading the page, not too much about XML or even HTTP.

Request is HTTP rather than XML

Another common mistake is to think that XML is used behind the scenes-frankly, I thought so! However, this view suggests that you have little knowledge of the technology. When a user opens a browser to request a Web page from the server, it enters something like http://www.google.com or http://www.headfirstlabs.com. Even if you do not enter http://, the browser will also add this part of the address bar. The first, http://, is a very intuitive clue about how to communicate: HTTP via Hypertext Transfer Protocol. When you write code in a Web page to communicate with the server, whether you use Ajax or a normal form POST or even a hyperlink, you are dealing with HTTP.

Now that all the WEB traffic between the browser and the server is done over HTTP, it makes no sense to think that XML is some kind of transmission technology that xmlhttprequest behind the scenes. Of course, XML can be sent in HTTP requests, but HTTP is a well-defined protocol that is unlikely to disappear for a short time. In addition to explicitly using XML in the request, or the server sending the response in XML, the XMLHttpRequest object uses just plain HTTP. So when someone says to you, "Oh, called XMLHttpRequest because you're using XML behind the scenes," you'd better laugh and patiently explain what HTTP is, telling them that although XML can be sent over HTTP, XML is a data format rather than a transport protocol. Through this discussion, deepen the understanding of it.

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.