Ajax Study Notes (2)-Must I use XML?

Source: Internet
Author: User

Is Ajax required to use XML? I don't think so.

Reasons for not using XML:
1. XML parsing using JavaScript scripts is slow;
2. For some simple data, it is a bit confusing to use XML;
3. XMLHTTP provides responsetext, which makes it easier for everyone to use XML;

What does XML not need to be used?
There are many techniques to use. For simple data, you can return custom data formats. For example, 1st bits are status bits and 2nd bits are data. For complex data, you can even directly return JavaScript scripts and execute them using eval.

In a recent chatProgramIn, I used the method of directly returning JavaScript scripts. below is the part for Automatically Obtaining messagesCode.

VaR XMLHTTP = createxh ();
Function checkmsg (){
XMLHTTP. Open ('get', 'message/checknew. aspx ', true );
XMLHTTP. onreadystatechange = continuecheckmsg;
XMLHTTP. Send ('');
}

Function continuecheckmsg (){
If (XMLHTTP. readystate = 4 ){
VaR RT = XMLHTTP. responsetext;
If (RT! = ''){
VaR msgs = eval (RT );
//... The next step is to process message distribution.
}
}
SetTimeout ('checkmsg () ', 10000); // refresh every 10 seconds
}

Note:
1. RT = XMLHTTP. responsetext returns the data of the new message. If there is no new message, null is returned. When there is a new message (multiple messages can be generated), the returned content format is as follows:
[['Sender 1', 'sender time 1', 'message content 1'], ['sender 2 ', 'sender time 2 ', 'message content 2'],...]
2. After var msgs = eval (RT) is used, msgs is a two-dimensional array that stores n messages and then processes them in two-dimensional array mode;

Finally, a useful article is collected.Article, Which provides detailed introduction to the use of XML:
Http://www.dragonson.com/doc/ajax.html

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.