XMLHttpRequest sending an XML request

Source: Internet
Author: User
Tags define function

1. Preface

For the case where the request parameter is master Key-value, the author prefers to use a simple post request. However, for some extreme forms, such as request parameters that are particularly numerous, and the structural relationships of request parameters are complex, you might consider sending an XML request. The essence of the XML request is the POST request, which simply encapsulates the request parameters in the form of an XML string in the client-side page that sends the request, and the server parses the XML string. Of course, after the server obtains the XML string, it can be parsed with tools such as dom4j or Jdom.

2. Example

This example is the same as before, through the left click on the country, and then click "Send", on the right side can see the country's cities. But this time, I'm paying to send in the form of an XML file. Source

1) Create a function of the XML document in the First.html page, and send it in <countrys><country>XXX</country></countrys> form.

Define function Createxml () {//Start creating XML document, Countrys is the root element var xml = "<countrys>";//Get first element, And get up all the child nodes (options) var options = document.getElementById ("First"). Childnodes;var option = null;//traverse all options for the country drop-down list for (var i = 0 ; i < options.length; i + +) {option = options[i];//if an option is selected if (option.selected) {//Adds a country child node under the root node of countrys XML = XML + "<country>" + Option.value + "<\/country>";}} Ends the XML document's root node XML = XML + "<\/countrys>";//returns XML document return XML;}

2) define the function to send

Defines the function that sends the XML request functions Send () {//initializes the XMLHttpRequest object Createxmlhttprequest ();//defines the Urlvar URI of the request sent = "second.jsp";// Open the connection to the server Xmlrequest.open ("POST", Uri, True);//Set the request header Xmlrequest.setrequestheader ("Content-type", "application/ X-www-form-urlencoded ")//specifies that the ProcessResponse function is triggered when the XMLHttpRequest object state changes Xmlrequest.onreadystatechange = processresponse;//Send XML Request Xmlrequest.send (Createxml ());}

3) define the callback function

Processing server response function ProcessResponse () {if (xmlrequest.readystate = = 4) {if (Xmlrequest.status = = 200) {//Get server response string, and to $ Split into multiple strings as delimiters//alert (Xmlrequest.responsetext), var citylist = Xmlrequest.responseText.split ("$"); var displayselect = document.getElementById ("second");//the option to empty the second drop-down list displayselect.innerhtml = Null;for (var i = 0; i < Citylist.lengt h; i++) {if (Citylist[i].trim (). length > 0) {//Create multiple option elements sequentially option = document.createelement ("option"); o ption.innerhtml = citylist[i];//Adds the created option element to the drop-down list at the end of displayselect.appendchild (option);}}}}

4) in the sencod.jsp page, use DOM4J to parse the XML format.

Defines a StringBuffer object that receives the request parameter StringBuffer Xmlbuffer = new StringBuffer (); String line = null;//Gets the input stream through the request object BufferedReader reader = Request.getreader ();//reads the data of the input stream in turn (line = Reader.readline ()) = null) {System.out.println ("= = =" +line); Xmlbuffer.append (line);} Converts the content read from the input stream to a string of strings XML = Xmlbuffer.tostring ();//Begins parsing XML string string document XmlDoc = new Xppreader () with dom4j. Read (New Byt Earrayinputstream (Xml.getbytes ()));//Get all child nodes of the countrys node list countrylist = Xmldoc.getrootelement (). elements ();

5) Running Results






XMLHttpRequest sending an XML request

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.