Be wary of using AJAX requests to detect non-existent resources

Source: Internet
Author: User
Tags error handling version client linux
Ajax| Request

Cited

Do not know how many of you remember Apress's "Foundations.of.Ajax" described in the head request pattern?

Yes, you can use XHR to send a request to a resource that you do not know is in and out of, thus detecting the existence of the period and making a corresponding judgment deal. It is possible to send any GET request, except that the head request is more lightweight and easier to use.

But is that actually the case?

Case

Technically I am a more radical faction, but also maintain the characteristics of young blood. So, at the time of new demand, I ventured to use the XHR head in order to prove the viability of the new technology.

The requirement is this:

We need to detect the status of the remote server in time to notify the client when the state of the server changes (for example, the machine just started).

In the past, such a need you can not use the web implementation. At most, you use a server-side program on a Web service to detect it and send it to a Web client. But the advent of Ajax makes this possible. By grasping the idea of Ajax, we have decisively designed a scheme that uses JavaScript to set up an application on the client, sending XHR requests to detect the state of the remote server, and doing the appropriate work.

It can be said that such a design is familiar with the essence of Ajax. The entry of XHR makes the traditional B/s program have the trend of developing to C/s. This design caters to this trend, and combines the requirements to achieve this function.

Problem

As the tests proceed, the problems ensue.

In fact, in the beginning of the design of this system is not without doing exploratory research. Since Ajax itself is a new technology, even if it is the old bottle of wine, but because the project has not been used before, no one will rashly adopt.

However, due to the hasty study, the tests did not cover all the case, leading to problems.

When the design is applied to the actual project, the problem is discovered gradually with the development of the test.

First of all, in the Mozilla 1.2.1 Environment found that the program has no corresponding. The main performance is that the program can run other functions, but XHR remote resources to detect the function can not be used. Why, then?

After investigation, we found that this is the problem.

Mozilla's lower-version browsers do support XHR, and we've had successful experiences before. The problem is that Mozilla's low version browser support for XHR is special when the request does not exist, as shown below.

When the request is unsuccessful, Mozilla 1.2.1 is requested in the following order:

1, ready_state_loading=1;

2, ready_state_loading=1;

3, ready_state_loaded=2;

After that, he disappeared.

Instead, our program has been waiting for the ready_state_complete=4 state, so the program is lost accordingly.

Logically, we can not rely on the ready_state_loaded state. Because in normal times, ready_state_loaded is also likely to occur. So there's no good way to do this with Mozilla's low version of browsers, the only way to do that is to set the time-out mechanism ourselves.

Then, as the test went deeper, the problem was discovered.

In the case of Firefox 1.5.0.4, the process of using XHR to detect resources in the far end is different. We found that Firefox works fine under win, and Firefox works very slowly under Linux. After testing, we found that in Linux under the Firefox environment, if you want to a remote non-existent resources to send a request, it will take about 400 seconds to return, and the same operation in the win version of Firefox but only dozens of seconds.

Only the length of time is different, let our application program France.

If the Mozilla low version of the browser due to the time earlier, its error handling process is also understandable. However, the same version of Firefox on different platforms to behave differently is too much to understand. But no way, the project requirements must be compatible with different platform browsers, and must be compatible with Mozilla, so ...

Incidentally, ie xhr no matter what operation is normal, you can rely on. and open source upstart SeaMonkey with his big Brother Firefox, the platform is very different.

Of course, some programs only require a certain platform, such as browser implementation, that said.

Summarize

The first one to eat crabs is always at risk. Success, everyone singing, failure, despite frustration, but we can not give up a chance to learn.

Summarized as follows:

Depending on the immutable factor, don't rely on change. In Ood, we usually talk about two principles, namely, the opening and closing principles and the Liskov principle. In fact, these two principles are combined to emphasize, to be oriented into the interface. Because interfaces are generally invariant, internal implementations are constantly changing. Here, although we are not ood, but there are some changes and the same factors. Generally speaking, people in the implementation of a common function, will be the success of the side to do a particularly good, more consistent, and personalized error handling, especially a new thing, the success of the side through the use of everyone will be unified, and error handling due to fewer people encounter, resulting in different forms. In the specific design, must be to the conditions of the factors to distinguish, carefully selected. If you rely on factors that are fast to change, you will cause the design to fail. Special consideration is given to the variable parts, such as error handling, and more effort will be devoted to the initial study. In the Prophase study, special consideration should be given to extreme situations, so as to improve the value of research and reduce risk.



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.