Home > Article > Web Front-end > Load and parse XML data using AJAX in jQuery
/******1,Content-Type
02 Many times the failure to parse is a problem with Content-Type.
03 If it is an xml file, please skip this step
04 The dynamically generated XML must be set to text/xml, otherwise the default is text/html, which is ordinary text.
05 Content-Type settings for common languages *********/
06
07 header("Content-Type:text/xml"); //php
08 response.ContentType="text/xml" //asp
09 response.setHeader("ContentType","text/xml"); //jsp
10 /******
11 2. xml structure.
12 XML must be closed, it is very important!
13 Example:
14 Wrong XML*********/
15
16
17
18
19
20
21
22 //正确的
23
24
25
26
27
28
29
30
31
32
33
34
35
36 /******
37 3. Analysis
38 Here is a reference to macnie’s
39 Traversing the student (the above XML is still used here, the child node is student)*********/
40
41
42 $.ajax({
43 url:'ajax.asp',
44 type: 'GET',
45 dataType: 'xml',//这里可以不写,但千万别写text或者html!!!
46 timeout: 1000,
47 error: function(xml){
48 alert('Error loading XML document'+xml);
49 },
50 success: function(xml){
51 $(xml).find("student").each(function(i){
52 var id=$(this).children("id"); //取对象
53 var idvalue=$(this).children("id").text(); //取文本
54 alert(id_value);//这里就是ID的值了。
55 alert($(this).attr("email")); //这里能显示student下的email属性。
56 //最后么输出了,这个是cssrain的写法,貌似比macnie更JQ一点
57 $('
')58 .html(id_value)
59 .appendTo('ol');
60 });
61 }
62 });
63
64 //One final addition: Make sure the server is encoded in utf-8, otherwise it will be garbled! Also make sure that your xml file is also in utf-8 format.
The above is the content of loading and parsing XML data using AJAX in jQuery. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!