<!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