1,content-type
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
Copy Code code as follows:
Header ("Content-type:text/xml"); Php
Response. Contenttype= "Text/xml"//asp
Response.setheader ("ContentType", "Text/xml"); Jsp
2,xml structure.
<xml must be closed to the >
Cases:
The wrong XML
Copy Code code as follows:
CODE:
<?xml version= "1.0" encoding= "UTF-8"?>
<name>zhangsan</name>
<id>1</id>
<name>lisi</name>
<id>2</id>
Right to
CODE:
<?xml version= "1.0" encoding= "UTF-8"?>
<stulist>
<student email= "1@1.com" >
<name>zhangsan</name>
<id>1</id>
</student>
<student email= "2@2.com" >
<name>lisi</name>
<id>2</id>
</student>
</stulist>
3, Analysis
This quote Macnie's
Traversing the student (here or using the XML above, the child node is student)
Copy Code code as follows:
$.ajax ({
URL: ' ajax.asp ',
Type: ' Get ',
DataType: ' xml ',//here can not write, but never write text or HTML!!!
timeout:1000,
Error:function (XML) {
Alert (' Error loading XML document ' +xml);
},
Success:function (XML) {
$ (XML). Find ("Student"). each (function (i) {
var id=$ (this). Children ("id"); Fetching objects
var id_value=$ (this). Children ("id"). text (); Fetching text
alert (id_value);//This is the value of the ID.
Alert ($ (this). attr ("email")); Here you can display the email attribute under student.
Finally, the output, this is the Cssrain, seemingly more jq than Macnie
$ (' <li></li> ')
. HTML (Id_value)
. Appendto (' ol ');
});
}
});
Finally add a: to ensure that the server side is UTF-8 encoding