Home > Web Front-end > JS Tutorial > Load and parse XML data using AJAX in jQuery

Load and parse XML data using AJAX in jQuery

黄舟
Release: 2016-12-21 15:07:55
Original
1017 people have browsed it

/******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    zhangsan    

18    1    

19    lisi    

20    2    

21    

22    //正确的    

23    

24        

25        

26        

27    zhangsan    

28    1    

29        

30        

31       lisi    

32    2    

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 (m.sbmmt.com)!


    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template