Use the POST method with caution when calling Ajax. There are serious problems or bugs.

Source: Internet
Author: User

This problem only exists when the client is an IE or IE kernel, but you cannot control the browser used by the client, so it will still cause a great performance problem for your application.

Let's talk about the phenomenon first:

Server: we only use a static html page OK .html, And the content only returns OK.

Then we will write an Ajax-called HTML for access on the client:

<script>function send_request(method,url,param) {    http_request = false;    if (window.XMLHttpRequest) {        http_request = new XMLHttpRequest();    } else if (window.ActiveXObject) {        try {            http_request = new ActiveXObject("Msxml2.XMLHTTP");        } catch (e) {            try {                http_request = new ActiveXObject("Microsoft.XMLHTTP");            } catch (e) {            }        }    }    if (!http_request) {        return;    }    http_request.open(method, url,true);    http_request.send(param);}function get(){    var url = "http://myhost/ok.html?name=axman&test=123";    for(var i=0;i<1000;i++){        send_request("GET",url,null);    }}function post(){    var url = "http://myhost/ok.html";    for(var i=0;i<1000;i++){        send_request("POST",url,'name=axman&test=123');    }}</script><input type="button" name="b1" value="get" onclick="get()"><input type="button" name="b2" value="post" onclick="post()">

The two access methods on this page are exactly the same as the parameters passed. When I press the get button, the server receives 1000 access records.

[Admin @ VM-platform access_log] $ cat apache_access.log.4 | WC-l
1000
Then I will count the records with access time greater than 1 ms:

[Admin @ VM-platform access_log] $ cat apache_access.log.4 | awk '{if ($12> 1000) Print $0 }'

The result is null.

Now press the post button:

[Admin @ VM-platform access_log] $ cat apache_access.log.4 | WC-l
2000

When 2000 is displayed on the server, the access is completed.

[Admin @ VM-platform access_log] $ cat apache_access.log.4 | awk '{if ($12> 1000) Print $0}' result:

10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696078 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696045 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 695776 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 695332 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696500 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696484 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696215 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696235 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 696182 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 55 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 694964 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 58 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 1274 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-
10.16.14.82---[24/NOV/2011: 16: 08: 59 + 0800] "post/myhost/OK .html HTTP/1.1" 200 20 1019 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0) "--" A =-; B =-; C = -"-

Note that Apache % d records the time from the first domain (starting from the first line feed and carriage return) to the response output (protocol layer, as long as it is written to the buff, not necessarily already sent to the client ). This time unit is

Microseconds. Check that 10 of the records are nearly 700 ms, thousands of times the get method.

This problem has caused performance problems for our two applications. I began to analyze this cause. I found the article from the Internet that the IE kernel browser was used for Ajax calling, the post method is sent in two steps. The first request is sent.

Header field. The second request is sent to the body. There is a large delay in the middle, and sometimes it will cause the loss of the body package. We can see from the network packet capture situation that only the header is sent, followed by only the error packet returned by the server timeout, there is no body data. This probability is about 1%.

 

When apache acts as a proxy, the proxy cannot send client requests to backend within the effective time, resulting in a large number of internal error outputs. If it is SS, it will also cause confusion in the data sequence. After \ r \ n is sent, the header and tail are not completely sent. These phenomena are all monitored from network packet capture.

If you change to Firefox, there will be no problem in executing post with the same Ajax code. The key is that you have no control over the use of IE.

The solution provided on the Internet does not work at all (maybe IE6 can work, and IE6 is not tested in detail in IE8). The key is that as long as this happens in Some browsers, this will bring a lot of performance problems to the application.

Therefore, try to use Ajax post to submit data as little as possible. However, sometimes the server only allows post submission for security reasons. You can dynamically construct form forms and use ajax to call post as little as possible.

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.