Ajax Series 4: problem summary

Source: Internet
Author: User

1. the most classic problem is the cache problem in IE. 
If get is used, a cache problem occurs in IE. The code is executed only once. The solution is to add a timestamp or random number to make the URL unique so that no IE
Or change to post for submission.
Xhr. Open ("get", "XXXX. aspx? _ Dc = "+ newdate (). gettime (), true );

2. Case sensitivity of Ajax Object Attributes
The W3C browser, such as ff, is case sensitive. For example
If (xhr. readystate = 4) is true in IE, but it won't work in FF because IE is case insensitive and FF is case sensitive.
The standard format is if (xhr. readystate = 4). Similarly, there are attributes responsetext, responsexml, and status.
Also, the status conversion function xhr. onreadystatechange must be in lowercase.

3. Ajax status 0 
In some cases, xhr. Status = 200 is added to test the Ajax code and the code of xhr. Status = 200 is not executed, so pay attention to this.
Xhr. status = 200 is to be browsed through the server, and the server page does not have an error or returns the 200 status only when it is switched, this status is consistent with the status defined by the server when you access the page through a browser.
Drag the browser directly to view the result or double-click the HTML page to run it. If no error occurs, xhr. Status is 0, not 200.
Therefore, you can add an xhr. Status = 0. As follows:

Copy codeThe Code is as follows:


If (xhr. Status = 200 | xhr. Status = 0 ){
Alert ('OK ');
}


Another problem occurs when you drag the browser to view the result or double-click the HTML page. If you are requesting an XML file, you must use the responsexml attribute to return the xmldom, however, the xmldom attribute cannot be returned in IE. The solution is as follows.
4. responsexml problems. 
To use the responsexml attribute, the request is an XML file or a dynamic page with the response header set to "text/XML. Note that if a dynamic page is requested, do not forget to set contenttype to "text/XML "!!!!!!!! Remember ~~~~~~
ASP is response. contenttype = "text/html"
Asp.net is response. contenttype = "text/html ";
PHP is the header ("Content-Type: text/XML ;");
If there is a problem in IE, when you drag it directly into the browser or double-click to run the HTML preview effect, even if the requested XML file, the xmldom cannot be returned using responsexml.
You will know after testing, as shown below:
Showbo. xml

Copy codeThe Code is as follows:


<Showbo>
<Item> 1 item>
<Item> 2 item>
<Item> 3 item>
<Item> 4 item>
</Showbo>


Test.html

Copy codeThe Code is as follows:


Function getajax (){
If (window. XMLHttpRequest) return New XMLHttpRequest ();
Else if (window. activexobject) return newactivexobject ("Microsoft. XMLHTTP ");
}
VaR xhr = getajax ();
Xhr. onreadystatechange = function (){
If (xhr. readystate = 4 ){
If (xhr. Status = 200 | xhr. Status = 0 ){
VaR Doc = xhr. responsexml, item = Doc. getelementsbytagname ("item ");
Alert (item. Length); // output in IE is 0, and output in FF is 4. The XML tree structure does not appear to be generated in IE. The specific reason is ms ..
}
Else alert ('error \ n \ n' + xhr. status );
}
}
Xhr. Open ("get", "showbo. xml? _ Dc = "+ newdate (). gettime (), true );
Xhr. Send (null );


The solution is to use the Microsoft. xmldom object to recreate the XML tree structure, as shown below:

Copy codeThe Code is as follows:


Xhr. onreadystatechange = function (){
If (xhr. readystate = 4 ){
If (xhr. Status = 200 | xhr. Status = 0 ){
VaR Doc = xhr. responsexml;
If (document. All & xhr. Status = 0) {// restructured the XML tree structure when IE is directly inserted into the browser.
Doc = new activexobject ("Microsoft. xmldom ");
Doc. loadxml (xhr. responsetext );
Doc=doc.doc umentelement;
}
VaR item = Doc. getelementsbytagname ("item ");
Alert (item. Length );
}
Else alert ('error \ n \ n' + xhr. status );
}
}


5. Note the following when submitting a POST request.
When submitting a POST request, you must set Content-Type to "application/X-WWW-form-urlencoded" so that you can use request/request on a dynamic page. form/request. the querystring object obtains the value through the key. Otherwise, the binary data must be used. Then, you can analyze the binary data to generate a String object and obtain the corresponding value using the regular expression.

Ajax Series 4: problem summary

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.