Ajax Series 4: problem summary

Source: Internet
Author: User

Ajax Series 4: problem summary

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:



1 item>
2 item>
3 item>
4 item>


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.

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.