Use Ajax requests with caution to detect nonexistent Resources
Introduction
I don't know how many of you remember the pattern of the head request described in foundations. Of. Ajax of apress?
Yes, you can use xhr to send a request to a resource that you do not know is not in use, so as to detect the existence of the resource and make corresponding judgment. In fact, you can send any GET request, but it is generally considered that the head request is lightweight and easy to use.
But is it actually like this?
Case
Technically, I am a radical school and maintain the characteristics of young people. So when the new demand came, I took the xhr head approach to prove the feasibility of the new technology.
The requirement is as follows:
We need to know the status of the remote server in time to notify the client users when the server status changes (for example, the machine is just started.
In the past, you were unable to implement such requirements using web. At most, you use a server program on the Web server to discover and then send it to the Web Client. However, the arrival of Ajax makes this possible. By understanding the idea of Ajax, We have decisively designed a scheme, using JavaScript to establish an application on the client, sending xhr requests to identify the status of the remote server, and complete the corresponding operations.
It can be said that such a design understands the essence of Ajax. With the introduction of xhr, traditional B/S programs are evolving towards C/S. This design meets this trend and meets the needs to implement this function.
Problem
As the test progresses, the problem arises.
In fact, there was no exploratory research at the beginning of the design of this system. Because Ajax is a new technology, even if it is a new bottle of old wine, but because the project has not been used before, no one will rush to use it.
However, due to the rush of research, the test did not cover all cases, resulting in problems.
After the design is applied to the actual project, problems are gradually discovered with the development of the test.
First A 1.2.1 In the environment, the program is found to have no corresponding results. The main manifestation is that other functions of the program can be run, but xhr cannot be used to detect remote resources. Why?
After investigation, we found that the problem was like this.
Earlier Mozilla browsers support xhr, And we have successful experiences. The problem is that the xhr support of earlier Mozilla browsers is somewhat special when there is no resource in the request, as shown below.
If the request fails A 1.2.1 The request order is as follows:
1. ready_state_loading = 1;
2. ready_state_loading = 1;
3. ready_state_loaded = 2;
Then it disappears.
On the contrary, our program has been waiting for the ready_state_complete = 4; state, so the program has lost the corresponding.
Logically, we cannot rely on the ready_state_loaded status. In normal cases, ready_state_loaded may also appear. Therefore, there is no good way to solve this problem in earlier versions of Mozilla browsers. The only way is to set the timeout mechanism by yourself.
Later, with the in-depth test, we found the problem.
Only use Firefox 1.5.0 4. For example, the process of using xhr to detect non-existent resources at the remote end is also different. We found that Firefox in Windows works normally, while Firefox in Linux works very slowly. After testing, we found that in the Firefox environment in Linux, if you want to send a request to a remote resource that does not exist, it takes about 400 seconds to return the request, the same operation takes dozens of seconds on Windows Firefox.
The difference in time alone makes our application solutions unrecognizable.
If the earlier version of the Mozilla Browser is used, the error handling process is quite understandable. However, Firefox of the same version has different performance on different platforms, which is hard to understand. But there is no way, the project must be compatible with browsers on different platforms, and be compatible with Mozilla, so ......
By the way, the xhr of IE is normal and can depend on any operation. The performance of the open-source SeaMonkey is the same as that of Firefox, his eldest brother. The platform is quite different.
Of course, some programs only need to be implemented on a platform or browser.
Summary
The first person who eats crabs is always at risk. If we succeed, everyone will praise us. If we fail, we cannot give up a learning opportunity even though we are depressed.
Summary:
It depends on constant factors and does not depend on changes. We usually talk about two principles in the conduct of OOD: The Open and Close principles and the liskov principles. In fact, these two principles should be combined to emphasize the need to turn interface-oriented. Because the interface is always unchanged, and the internal implementation changes frequently. Here, although we are not good, there are also some changing and changing factors. Generally, when people implement a common function, they will make the successful side better and more consistent, and personalize the error handling, especially a new thing, the successful side will be unified through everyone's use, and the error handling results may vary due to a small number of attacks. During the specific design, we must distinguish the conditions from each other and select them with caution. If you rely on rapid changes, the design will fail. Special considerations for the variable parts, such as error handling, require more energy in the preliminary research. Extreme situations should also be taken into consideration during preliminary studies to increase the value of research and reduce risks.