Ajax basics-Chapter 3)

Source: Internet
Author: User

Parse the response to XML: 

The server does not necessarily send responses in XML format. As long as the Content-Type response header is set to text/pain (for XML, the Content-Type header is text/XML ).

XML preface:Defines the XML version and the font encoding used in the document

<? XML version = "1.0" encoding = "UTF-8"?>

(Gb2312 (Simplified Chinese), UTF-8 is the world's universal language coding ,. utf8 is short for the Unicode octal interchange format. Unicode is an international standard and equivalent standard of ISO 10646. The gb2312, gb18030, and GBK standards commonly used in simplified Chinese versions in mainland China are China's national standards, but they can only encode Chinese and most western texts. Utf8 encoding is a better choice for the purpose of universality of websites.

XMLHTTP. Status = 0:

XMLHTTP. Status = 4 indicates running through the server, and 0 indicates running on the local machine, even if not through the server (such as Tomcat)

Example:Parsexml. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<States>
<North>
<State> Minnesota </State>
<State> Iowa </State>
<State> North Dakota </State>
</North>
<South>
<State> Texas </State>
<State> Oklahoma </State>
<State> Louisiana </State>
</South>
<East>
<State> New York </State>
<State> North Branch Lina </State>
<State> mascript usetts </State>
</East>
<West>
<State> California </State>
<State> Oregon </State>
<State> Nevada </State>
</West>
</States>

Parsexml.html:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" contect = "text/XML"; charsets = UTF-8 ">
<Title> parsing XML responses with the W3C Dom </title>
<SCRIPT type = "text/JavaScript">
VaR XMLHTTP;
VaR requesttype = "";

Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}

Function startrequest (requestedlist ){
Requesttype = requestedlist;
Createxmlhttprequest ();
XMLHTTP. onreadystatechange = handlestatechange;

XMLHTTP. Open ("get", "parsexml. xml", true );
XMLHTTP. Send (null );
}

Function handlestatechange (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 | XMLHTTP. Status = 0 ){
If (requesttype = "North "){
Listnorthstates ();
}
Else if (requesttype = "all "){
Listallstates ();
}
}
}
}
 
Function listnorthstates (){
VaR xmldoc = XMLHTTP. responsexml;
VaR northnode = xmldoc. getelementsbytagname ("North") [0];

VaR out = "Northern States ";
VaR northstates = northnode. getelementsbytagname ("state ");

Outputlist ("Northern States", northstates );
}

Function listallstates (){
VaR xmldoc = XMLHTTP. responsexml;
VaR allstates = xmldoc. getelementsbytagname ("state ");

Outputlist ("all States in Document", allstates );
}

Function outputlist (title, States ){
VaR out = title;
VaR currentstate = NULL;
For (VAR I = 0; I <states. length; I ++ ){
Currentstate = States [I];
Out = out + "/n-" + currentstate. childnodes [0]. nodevalue;
}

Alert (out );
}
</SCRIPT>
</Head>

<Body>
<H1> process XML document of U. S. States <Br/>
<Form action = "#">
<Input type = "button" value = "View All Listed States" onclick = "startrequest ('all');"/>
<Br/>
<Input type = "button" value = "view all listed northern states" onclick = "startrequest ('north');"/>
</Form>
</Body>
</Html>

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.