There is a strange problem when you reuse XMLHttpRequest for post data under Chrome.

Source: Internet
Author: User

var http_request; //is outside the declaration object, mainly in order to use    //in Updatepage without refreshing the content function post (URL, Parameter) {       if (http_request==null)     {         http_request = gethttprequest ();     }            if  (!http_request)        {        alert ("xmlhttprequest  initialization failed!");     }    else    {         //adds a rnd random number at the end of the URL so that it does not occur to read IE cache         if ( Url.indexof ("?")  > -1)         {             url = url +  "&rnd="  + math.random ();         }        else         {            url = url +   "? rnd="  + math.random ();        }                        http_request.open ("POST",  url, false);  //Open Request  //false  Synchronous, true  Async         http_request.onreadystatechange = updatepage; / /Set callback method         http_request.setrequestheader ("Content-length",  Parameter.length);         http_request.setrequestheader (' Content-Type ') ,  ' application/x-www-form-urlencoded ');         http_request.send ( parameter);  //Send Request      }}  //callback Method Function updatepage () {       if   (http_request.readystate == 4)     {            if (http_request.status == 200)                    {                      alert ("...");  / /        }    }}

The test found that, under Chrome 26, if a public variable was declared: Http_request

Then when the XMLHttpRequest is reused multiple times, if the parameter (content) is passed at post, the callback function is executed 2 times in a row: Updatepage ()

If the post has no arguments (content), only 1 callback functions will be executed: Updatepage ()

Tested under Firefox and IE, only 1 callback functions are performed: Updatepage ()

It's also easy to solve this problem with Chrome 26, which is re-initialized each time you use Http_request (XMLHttpRequest):

Http_request = Gethttprequest ();

There is a strange problem when you reuse XMLHttpRequest for post data under Chrome.

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.