jquery Ajax Load XML document

Source: Internet
Author: User

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>jquery Ajax loading XML documents </title>
<script language= "Web Effects" >
$.ajax ({
This is omnipotent, not only can read XML
URL: ' i.xml ',//xml document Path
Type: ' Get ',//Request method
DataType: ' xml ',//document type
timeout:1000,//Super Long
cache:false,//whether to cache
Error:function () {alert (' oh,oh,error!haha! ');},//This is a message that throws a load failure, it's smarter than JS.
Success:function (XML) {
Alert (' yeah! success! '); /perform operations on the contents of the XML document here
}
});
A more concise approach
$.get (' I.xml ', function (XML) {
Alert (' yeah! success! '); /perform operations on the contents of the XML document here
});

</script>

Method Two

<script type= "Text/javascript" >
$ (function () {

$ ("#ajax"). One (' click ', Function () {
//$ ("#ajax"). Nextall (). remove ();
$.ajax ({
URL: ' ajax.xml ',
// Data: {id:1, name:0},
DataType: ' xml ',
Error:function (XML) {
Alert (' Load error ');
},
Success:function (XML {
var tr= ';
$ (XML). Find (' Row '). each (function (i) {
var Id=$.trim ($ (this). Children ("id"). text ());//Fetch Object
var v1=$ (this) . attr (' v1 ');
var v2=$ (this). attr (' v2 ');
var v3=$ (this). attr (' v3 ');
var v4=$ (this). attr (' v4 ');
var v5=$ (this). attr (' V5 ');
//Last Output
$ (' <tr></tr> ')
. html (' <td>id: ' +id+ ' </td><td>v1: ' +v1+ ' </td ><td>v2: ' +v2+ ' </td><td>v3: ' +v3+ ' </td><td>v4: ' +v4+ ' </td><td>v5: ' + v5+ ' </td> ')
. Appendto ("#list");

});
var tabl= ' <table bgcolor= "#ffffff" border= "0" cellpadding= "1" cellspacing= "1" width= "100%" ">";
var tab2= ' </table> ';
$ ("#list"). HTML (tabl + TR + tab2);
}
})
});
$ ("#ajax"). Click (function () {$ (this). Nextall (). Toggle ();});
$ ("#t1"). Click (function () {$ (this). Next (). Toggle ();});

})
</script>

<body>
</body>

Many times the inability to parse is the content-type problem. If you are an XML file, skip this step.
Dynamically generated XML must be set to Text/xml, otherwise the default is text/html is ordinary text. Content-type settings for common languages:
Response.setheader ("contenttype", "Text/xml"); JSP Tutorials
Response.contenttype= "Text/xml"; ASP Tutorials
Header ("Content-type:text/xml"); PHP Tutorials

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.