Jquery Ajax XML instance

Source: Internet
Author: User
Jquery Ajax XML instance

Example 1:
========================================================== ==================
XML content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Stulist>
<Man>
<Name> zdz </Name>
<Age> 25 </age>
</Man>
<Man>
<Name> Liufeng </Name>
<Age> 22 </age>
</Man>
<Man>
<Name> hotman </Name>
<Age> 18 </age>
</Man>
</Stulist>

============================================
<SCRIPT>
$ (Document). Ready (function (){
$. Ajax ({URL: "test. xml", datatype: "XML", success: function (XML ){
$ (XML). Find ("stulist> Man"). Each (function (){
Document. getelementbyid ("zdzdiv "). innerhtml + = "<br> name:" + $ (this ). find ("name "). text () + "<br/> Age:" + $ (this ). find ("Age "). text ();
});
}});
})

</SCRIPT>
<Div id = "zdzdiv">
============================================
Parameter 1 URL: Path of the document to be requested.

Parameter 2 datatype: the requested document type. xml HTML Script JSON and so on.

Parameter 3 success: the callback function after the request is successful.

$ (XML). Find ("stulist> Man"). Each finds the man tag under the stulist node in the returned XML, and executes the function based on the search result.

Search for child elements under the man tag and place the values in the displayed Div.
========================================================== ========================================================== ============
Example 2:
Function resultxml ()
{
$. Ajax
({
URL: 'ajaxrespose. aspx ',
Data: {ID: 1, name: $ ('# gender'). Val ()},
Datatype: 'xml ',
Success: function (XML)
{
Alert (XML );

$ (XML). Find ('select'). Each (function ()
{
VaR option1 = "<option value = '";
VaR option2 = "'> ";
VaR option3 = "</option> ";
VaR text = $ (this). Children ("text"). Text ();
VaR value = $ (this). Children ("value"). Text ();
$ ("# City"). append (option1 + value + option2 + TEXT + option3 );

});

}
});
}
XML file returned by the server
String targetid = request. querystring ["ID"]; // obtain the value of the parameter ID in the request.
String xml_version = "<? XML version =/"1.0/" encoding =/"UTF-8/"?> ";
String xml_start = "<selects> ";
String xml_end = "</selects> ";
String xml = "";

Switch (targetid)
{
Case "0 ":
Xml = "<SELECT> <value> 0 </value> <text> select </text> </SELECT> ";

Break;
Case "1 ":
Xml = "<SELECT> <value> 1 </value> <text> Changping </text> </SELECT> ";
XML + = "<SELECT> <value> 2 </value> <text> Fengtai </text> </SELECT> ";
Break;
Case "2 ":
Xml = "<SELECT> <value> 1 </value> <text> Tanggu District </text> </SELECT> ";
XML + = "<SELECT> <value> 2 </value> <text> hangu </text> </SELECT> ";
Break;
}

String last_xml = xml_version + xml_start + XML + xml_end;
Response. contenttype = "text/XML"; // The returned data type is XML (required)
Response. Write (last_xml );
Response. End ();

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.