JS Ajax transfer XML DOM object to server

Source: Internet
Author: User

Client Code
1<script>2 varIsie =true;3 varXMLHTTP =NULL;4 functionCreatexmlhttp () {//Create a Xmlxmlhttprequest object5 if(xmlhttp = =NULL) {6 if(window. XMLHttpRequest) {7XMLHTTP =NewXMLHttpRequest ();8 }9 Else {TenXMLHTTP =NewActiveXObject ("Microsoft.XMLHTTP"); One } A //if (xmlhttp = = null) { - //alert (' Your browser does not support Ajax '); - //} the } - if($.browser.msie) {//IE Browser -Isie =true; - } + Else { -Isie =false; + } A at } - //creating an XML document that object is also the first step - functionCreatedomxml () { - varDomxml =NULL; - //IE Browser creates an XML DOM object - varsigns = ["msxml2.domdocument.5.0", "msxml2.domdocument.4.0", in -"msxml2.domdocument.3.0", "Msxml2.domdocument", "Microsoft.XMLDOM"]; to for(vari = 0; i < signs.length; i++) { + Try { -Domxml =NewActiveXObject (Signs[i]); the Break; * } $ Catch(e) {}Panax Notoginseng } - the //Other Browsers + if(Domxml = =NULL) ADomxml = Document.implementation.createDocument ("", "",NULL); the returnDomxml; + } - //The second step: The client gets the data to write in the XML Document Object $ functionCreatexml (DOM) { $ varRoot = doc.createelement ("root");//Create a root node - varassign_id = doc.createelement ("id");//child Nodes - varAssign_ans = doc.createelement ("answer");//child Nodes the if(Isie) { -Assign_id.text = "1";WuyiAssign_ans.text = "1"; the } - Else { WuAssign_id.textcontent = "1"; -Assign_ans.textcontent = "1"; About } $ - - Root.appendchild (assign_id); - A Root.appendchild (Assign_ans); + the dom.appendchild (choose); - } $ functionSubmit () { the varDom =createdomxml (); the createxmlhttp (); the if(xmlhttp = =NULL) { theAlert ("Your browser does not support Ajax, please try a different browser"); - return0; in } the Createxml (DOM); the AboutXmlhttp.onreadystatechange =success; theXmlhttp.open ("Post", "Test.aspx",true);//Receive Request page the //Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); the Xmlhttp.send (DOM); + } - the functionSuccess ()//Asynchronous handler function when the service returns successfullyBayi { the if(Xmlhttp.readystate = = 4) { the alert (xmlhttp.status); - if(Xmlhttp.status = = 200) { - alert (xmlhttp.responsetext); the } the Else { theAlert ("Test Error"); the } - } the } the</script>
Server-side code
1ProtectedvoidPage_Load (object sender, EventArgs e)2 {3 string res;4request.contentencoding = System.Text.Encoding.GetEncoding ("UTF-8");5XmlDocument xmldoc =NewXmlDocument ();6 xmldoc.load (request.inputstream);7XmlNode node = xmldoc.selectsinglenode ("Choose");8foreach (XmlNode childinchnode. ChildNodes)9 {TenRes +Child.innertext; One } A Response.Write (res); - Response.End (); - } the}

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.