Data format for the response of Ajax:
Text, a piece of HTML data, Xml,json.
To generate XML files using tools:
You typically use the XStream tool to convert collections, arrays, and objects into XML format.
Step One: Import XStream Toolkit: Xpp3_min-1.1.4c.jar;xstream-1.4.4.jar;
Step two: Use of XStream:
list<city> list =ps.searchcitybypid (PID); XStream XStream=NewXStream ();//Create a XStream objectXstream.alias ("City"), City.class);//set to replace the full path of the object with the string you specified//If you do not make the following settings, then the relationship between the object and the object member variable is the same as the parent tag and child tag in XML after the conversion to XML;//The relationship between object and object member variables after conversion to XML is set as follows if there is more than one attribute in a label in the XML;//xstream.useattributefor (City.class, "cname");//sets the object's target member variable after conversion to the tag attribute in XML. //xstream.useattributefor (City.class, "CID");String Xmlstr =xstream.toxml (list); //This is set to Text/xml; then the data type of the front page echo is [object XMLDocument]//If this is set to text/html, then the front end is the HTML tag that is the stringResponse.setcontenttype ("Text/xml;charset=utf-8"); Response.getwriter (). print (XMLSTR);
Parse the XML file on the client:
1 <HTML>2 <Head>3 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">4 <title>Hello Miss Dang</title>5 </Head>6 <Scriptsrc= "${pagecontext.request.contextpath}/js/jquery-1.11.3.min.js"></Script>7 <Scripttype= "Text/javascript">8 $(function(){9 $("#province"). Change (function(){Ten varPID= $( This). Val (); One $.post ("${pagecontext.request.contextpath}/citysservlet",{"PID":p ID},function(data) { A alert (data); - $("#city"). HTML ("<option>-Please select City-</option>"); - $ (data). Find (" City"). each (function(){ the varCID= $( This). Children ("CID"). text (); - varCNAME= $( This). Children ("CNAME"). text (); - $("#city"). Append ("<option value = '"+CID+"' >"+CNAME+"</option>"); - }); + }); - }); + }); A at </Script> - <Body> - <form> - <SelectID= "Province"> - <option>-Please select province-</option> - <C:foreachvar= "I"Items= "${list}"> in <optionvalue= "${i.pid}">${I.pname}</option> - </C:foreach> to </Select> + <SelectID= "City"> - the </Select> * </form> $ </Body>Panax Notoginseng </HTML>
jquery implementation of AJAX asynchronous request implementation of provincial and municipal linkage (data transfer format XML)