Sometimes, the server cannot respond to too many requests.
You need to understand before in-depth study of the technology for retrieving XML responses from the server, why is it a good idea to make the server send XML to respond to the request? This is different from the reason why the client sends XML requests ).
The client sends a request by name/value
Recall the previous article and you will know that in most cases, the client does not need to use XML because they will use name/value pairs to send requests. Therefore, you may send a name like this: name = jennifer. Simply add the "and" Symbol &) between consecutive name/value pairs to put them together, like this: name = jennifer & job = president. With simple text and these name-value pairs, the client can easily request multiple values from the server. The extra structures provided by XML and their extra overhead are rarely used ).
In fact, all the reasons for sending XML to the server can be classified into the following two basic categories:
- The server only accepts XML requests.In this case, you have no choice. The basic knowledge introduced in the previous article should have given you the tools necessary to send such requests.
- You are calling a Remote API that only accepts XML or SOAP requests.This is actually a special case of the previous case, but it is worth mentioning separately. If you want to use APIs from Google or Amazon in an asynchronous request, there are some special considerations. In the next article, I will introduce these considerations and provide examples of sending such requests to APIs.
The server cannot send name/value pairs in a standard way.
When you send a name/value pair, the Web browser sends a request, the platform responds to the request, and carries a server program, use it to convert the name/value pairs into data that can be easily processed by the server program. In fact, every server-side technology-from Java™Servlet to PHP, Perl, Ruby on Rails-allow you to call multiple methods to obtain values by name. Therefore, retrieving the name attribute is only a small matter.
This situation does not lead us to another direction. If the server uses the string name = jennifer & job = president to respond to an application, the client does not have any standardized and easy way to split each pair into names and values. You must manually parse the returned data. If the server returns a response consisting of name/value pairs, the explanation difficulty of the response is the same as that of using semicolons, vertical bars, or any other non-standard formatting characters.
For you, this means that there is no simple way to use plain text in the response, so that the client can get and interpret the response in a standard way, this is the case when the response contains at least multiple values. If your server only needs to send the number 42 back, plain text is a good choice. But what should I do if the server sends back the TV series Lost, Alias and Six Degrees for one time? Although you can select many methods to send this response using plain text, listing 1 provides some examples), there is no extremely simple method that does not require some processing by the client, there is no standardization method.
List 1. servers with different ratings respond to different versions)
show=Alias&ratings=6.5|show=Lost&ratings=14.2|show =Six%20Degrees&ratings=9.1Alias=6.5&Lost=14.2&Six%20Degrees=9.1Alias|6.5|Lost|14.2|Six%20Degrees|9.1 |
Although it is not difficult to find a way to split these response strings, the client will have to parse and split these strings Based on semicolons, equal signs, vertical bars, and symbols. This is not a way to compile robust code that allows other developers to easily understand and maintain.
Enter XML
Realize that there is no standard way to make the server use the name/value pair to respond to the client, the reason for using XML is obvious. When sending data to the client, the name/value pair is a good choice, because the server and the server language can easily interpret the name/value pair; the same is true for returning data to the client using XML. In the first several articles in this series, you have seen the use of DOM to parse XML. In subsequent articles, you can also see how JSON provides an alternative for parsing XML. You can process XML as plain text and obtain its value in this way. Therefore, there are several methods to obtain XML responses from the server, extract data using standard code, and use the data in the client.
XML is easy to understand. For example, most programmers can understand the data in Listing 2.
List 2. Server Response XML format for rating)
Alias
6.5
Lost
14.2
Six Degrees
9.1
|
In terms of the meaning of a specific semicolon or marker, the code in Listing 2 is not concealed.
Receive XML from server
Because the focus of this series is on the Ajax application mode client, I will not go into details about how server programs can generate XML responses. But when your client receives XML, you need to understand some special considerations.
First, you can use two basic methods to process an XML response from the server:
- As plain text that happens to be formatted as XML
- As an XML Document, it is represented by a DOM Document Object.
Next, for example, suppose there is a simple XML response from the server. Listing 3 shows the list of program ratings that are the same as the content described above. In fact, it is the same XML as listing 2. Here again, it is just for your convenience ). I will use this sample XML in this part of the discussion.
Listing 3. Example of rating in XML format
Alias
6.5
Lost
14.2
Six Degrees
9.1
|
Process XML as plain text
The simplest choice for processing XML is at least to learn about new programming technologies). It is to process XML in the same way as other text fragments returned by the server. In other words, the data format is basically ignored, and only the server response is concerned.
In this case, you need to use the responseText attribute of the request object, as shown in Listing 4 when the server sends you a non-XML response ).
Listing 4. processing XML as a common Server Response
function updatePage() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; // response has the XML response from the server alert(response); } }} |
In this code snippet, updatePage () is the callback method, and request is the XMLHttpRequest object. In the end, you will get an XML response that concatenates everything in the response variable. If this variable is output, you will get a result similar to listing 5. Note that the code in listing 5 should normally be a continuous code line. The multiline format is used for normal display .)
Listing 5. response variable value
Alias
6.5
Lost
14.2
Six Degrees
9.1
|
Here, the most important thing to note is that XML runs as a whole. In most cases, the server does not use spaces and carriage return to format the XML, but concatenates everything, as you can see in listing 5. Of course, your application does not care too much about spaces, so this is not a problem, but it does make it difficult to read the code.
Here, you can use the JavaScript split function to split the data and obtain the element name and value through basic string operations. There is no doubt that this is a headache. It ignores the fact that you have spent a lot of time reading the DOMDocument Object Model in previous articles. Therefore, I want to emphasize that you should keep in mind that using responseText can easily use and output Server XML responses, but I will not show you too much code. When DOM can be used, you should not select this method to obtain XML data, as shown below.