Encounter with Ajax.

Source: Internet
Author: User
Encounter with Ajax.
Author: shellex (all-metal shell)
Context (this part is encrypted ):
^ * & $ * Ue * W & E * @ () $ * () @ $ @) ($ dwdww.
Environment:
Windows 2K3 SP1
* Firefox 2.0
* Operators' 9.10
* IE 7 beta 3

Problems and Solutions:
Problem 1: page data in opera and IE is not updated on a regular basis. according to setinterval ('refresh () ', 1000); after each refresh function is completed, you should be able to get new data from the server to update page elements, but only Firefox works normally, their browser data will keep the first data.
Solution 1: It is estimated that it is a cache problem.
Xmlrequest. Open ('get', file, true );
Change
Xmlrequest. Open ('post', file, true );
Opera is valid, and IE is invalid.
Add random parameters to clock. php on the Data Source Page:
Ajaxread ('clock. php? Id = '+ math. Random () * 10 );
Opera is valid, and IE is invalid.
Problem 2 discovered through debugging:
Question 2: xmlrequest. onreadystatechange is triggered only once. originally, according to setinterval ('refresh () ', 1000);, the refresh function should be executed several times and each time an XMLHttpRequest request is sent. but it does not work in IE. this may be the reason why the problem cannot be solved.
Solution 2: replace the original global XMLHTTPRequest object with the following:
Function ajaxread (File ){
VaR xmlrequest = createxmlobj ();
.....
}
Similarly, ie also has a cache problem, so test solution 1:
Xmlrequest. Open ('post', file, true );
Opera is valid, and IE is invalid.
Add random parameters to clock. php on the Data Source Page:
Ajaxread ('clock. php? Id = '+ math. Random () * 10 );
Effective for opera and effective for IE.
So Problem 1 and problem 2 are solved.
Question 3: Firefox and IE Parse XML. Clock. php returns a string in XML format. At first, I parsed it like this:
VaR xmldoc = xmlrequest. responsexml;
VaR root_node = xmldoc. getelementsbytagname ('time') [0];
Updata ('xmlobj ', root_node.firstchild.data );
Unfortunately, this method is successfully parsed Only in opera. In ff, firebugs prompts that the getelementsbytagname is incorrect. after testing in IE, I found that root_node is always null.
Solution 3:
For ff, this should be written after the XMLHTTPRequest object is created:
Xmlobj = new XMLHttpRequest ();
Try {
Xmlobj. overridemimetype ('text/xml ');
} Catch (e ){....}
If the server response does not contain the XML mime-type header, the subsequent xmldoc. getelementsbytagname ('time') cannot work normally.
If you do not add xmlobj. overridemimetype ('text/xml'), you can use domparser () to parse XML. The Code is as follows:
VaR parser = new domparser ();
VaR xmldoc = parser. parsefromstring (xmlrequest. responsetext, "text/XML ");
// Replace var xmldoc = xmlrequest. responsexml;
For IE, xmldom should be used:
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = "false ";
Xmldoc. loadxml (xmlrequest. responsetext );
If (! Window. activexobject )...{
VaR parser = new domparser ();
VaR xmldoc = parser. parsefromstring (xmlrequest. responsetext, "text/XML ");
// Var xmldoc = xmlrequest. responsexml;
} Else ...{
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = "false ";
Xmldoc. loadxml (xmlrequest. responsetext );
}
VaR root_node = xmldoc. getelementsbytagname ('time') [0];
Updata ('xmlobj ', root_node.firstchild.data );

Solution 3.
Well, record it and keep it as a memo.

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.