Ajax basic instance returns XML

Source: Internet
Author: User

// Ajax request for the City Data
Function ajaxforget (CN ){
VaR xmlhttp_request = "";
Try {
If (window. activexobject ){
For (VAR I = 5; I --){
Try {
If (I = 2 ){
Xmlhttp_request = new activexobject ("Microsoft. XMLHTTP ");
}
Else {
Xmlhttp_request = new activexobject ("msxml2.xmlhttp." + I + ". 0 ");
}
Break;
}
Catch (e ){
Xmlhttp_request = false;
}
}
}
Else if (window. XMLHttpRequest ){
Xmlhttp_request = new XMLHttpRequest ();
}
}
Catch (e) {xmlhttp_request = false ;}
If (! Xmlhttp_request ){
Alert ("your browser does not support Ajax ");
Return;
}
Xmlhttp_request.open ('get', '../handler/citydata. ashx? Code = '+ CN +' & time = '+ new date (). getmilliseconds (), true );

Xmlhttp_request.setrequestheader ("Content-Type", "text/XML"); // setRequestHeader ("Header", "value ")
Xmlhttp_request.setrequestheader ("charset", "gb2312"); // set the specified header to the provided value. You must call open () before setting any header ()
Xmlhttp_request.send (null );
VaR self = xmlhttp_request; // assign the reference to the temporary variable self
Xmlhttp_request.onreadystatechange = function (){
Thecallback (Self );
};
}

// Ajax callback function
Function thecallback (xmlhttp_request ){
If (xmlhttp_request.readystate = 4 & xmlhttp_request.status = 200 ){
// Bind item
VaR DATA = xmlhttp_request.responsexml;
VaR citys = data. getelementsbytagname ("name ");
VaR CNS = data. getelementsbytagname ("Code ");
VaR listcity = $ ("listcity ");
Listcity. innerhtml = ""; // remove all items first
VaR listp = $ ("listprovince ");
VaR code = listp. Options [listp. selectedindex]. value;
VaR item = new array ();
For (VAR I = 0; I <Citys. length; I ++ ){
If (Citys. length! = 1 & code = CNS [I]. childnodes [0]. Data) continue;
Item [I] = Document. createelement ("option ");
Item [I]. value = Central [I]. childnodes [0]. Data;
Item [I]. innerhtml = citys [I]. childnodes [0]. Data;
Listcity. appendchild (item [I]);
}
// End the bound item
$ ("Txtcode"). value = listcity. Options [0]. value;
}
}

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.