Using jquery to implement AJAX loading XML and parsing script _jquery

Source: Internet
Author: User
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
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.