jquery implementation of AJAX asynchronous request implementation of provincial and municipal linkage (data transfer format XML)

Source: Internet
Author: User

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)

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.